Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Find inactive objects with script

Discussion in 'Scripting' started by V_endeta, Jan 2, 2020.

  1. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
    I'm trying to create a system that will find all the items in-game.

    But some items are turned off when the game is loaded (pickuped items)
    Code (CSharp):
    1. SetAtive(false)
    And now when I try to find those items I get null
    Code (CSharp):
    1. GameObject[] objects = Resources.FindObjectsOfTypeAll(typeof(Item)) as GameObject[];
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    Code:
    Code (CSharp):
    1.     GameObject GetItemGameObject(Item item)
    2.     {
    3.         GameObject[] objects = Resources.FindObjectsOfTypeAll(typeof(Item)) as GameObject[];
    4.      
    5.         GameObject itemObj = new GameObject();
    6.  
    7.         foreach (var i in objects)// -NULL
    8.         {
    9.             if(i.GetComponent<Item>().universalID == item.universalID)
    10.             {
    11.                 itemObj = i.gameObject;
    12.             }
    13.         }
    14.         return itemObj;
    15.     }
     
    tkchu likes this.
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd recommend just adding all these items to a list when they are created. Either when they are instantiated, or in a Start method attached to the item. They will remain in the list when they are inactive, so you'd just iterate over the list.
     
  3. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    356
    You're casting the result to GameObject[] for a type of Item. Use Item[] instead. Also when you call new GameObject() you'll instantiate a new GameObject in the scene. Lastly, once you've found the item you can break out of the loop.
    Code (CSharp):
    1. GameObject GetItemGameObject(Item item)
    2. {
    3.     Item[] items = Resources.FindObjectsOfTypeAll(typeof(Item)) as Item[];
    4.  
    5.     GameObject itemObj = null;
    6.  
    7.     foreach (var i in items)
    8.     {
    9.         if (i.universalID == item.universalID)
    10.         {
    11.             itemObj = i.gameObject;
    12.             break;
    13.         }
    14.     }
    15.  
    16.     return itemObj;
    17. }
     
    V_endeta likes this.
  4. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Steedalion and LoneDev6 like this.
  5. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
    This work. Thanks !
     
  6. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
  7. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Explain further why you think it's of no use to you?

    https://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html
    They both return the same thing, except with FindObjectsOfType you get to pass in a parameter saying "give me inactive objects too".
     
  8. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
  9. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Yes, yes it can. I linked you to the docs page for it which tells you it can, AND gave you example code with how to do it.
     

    Attached Files:

  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    I was curious about this, because FindObjectsOfType has not been able to return inactive gameobjects before. It seems the 2019.2 docs listed this param, but their code itself still doesn't incorporate it. I'm using Unity 2019.3 and when I check possible options, the param version isn't available. Even if I include the true bool (as a test), it throws an error. Even Unity's doc doesn't show they take the bool as a param. Maybe this is for a future update, but for now, it appears resources is the one that has to be used.
     
  11. V_endeta

    V_endeta

    Joined:
    Mar 31, 2017
    Posts:
    28
    Like I said above. this function does not return disabled objects. It might say that in the documentation but it doesn't work (Unity 2019.3.0f1).
     
    Ne0mega likes this.
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    And I was verifying it since it is now listed in the docs as taking the bool param. It's certainly a much requested feature.
     
    elmarj and pt-paulrahme like this.
  13. XenomorphGames

    XenomorphGames

    Joined:
    Feb 14, 2018
    Posts:
    4
    i found a much eiser way for if you want your item to be picked up have it starting not bieng parented to the item manager the gameobject which is the parent to the items and when you pickup the item it makes it its parent using this:

    if (activateTrigger)
    {
    item.SetActive(true);
    item.transform.SetParent(parent);


    }
    parent is a public transform which you would specify which parent you want the item object to be a child to
     
  14. trofimpubg

    trofimpubg

    Joined:
    Jul 20, 2022
    Posts:
    1
    Make object active in inspector, then write on Void Start() gameObject.setActive(false)
    then make GameObject.Find() in Void Awake() This function is the same but called before start