Search Unity

Best way for to select gameobject ?

Discussion in 'Scripting' started by FraMarSaMi, Aug 23, 2014.

  1. FraMarSaMi

    FraMarSaMi

    Joined:
    May 13, 2014
    Posts:
    86
    What is the best way for to select gameobject ? If I use GameObject.Find if the object isn't active unity display error..... What is the method that you use ?

    Thank you
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    GameObject.Find() only returns active game objects.
     
  3. FraMarSaMi

    FraMarSaMi

    Joined:
    May 13, 2014
    Posts:
    86
    Then how can you select the GameObjects not active? Someone stores the gameobject's instances into static classes/variables but I don't know if it's correct...
     
  4. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    One option is to use the extremely expensive FindObjectsOfTypeAll, but that is not recommended.

    Instead, you could create a static object which holds all the game objects of your choice. Then you can add/remove game objects from it on each of the game objects' OnEnable() and OnDisable().

    However, chances are you are doing something wrong if you need to find inactive game objects in your scene. Why do you have to do it?
     
  5. FraMarSaMi

    FraMarSaMi

    Joined:
    May 13, 2014
    Posts:
    86
    I have some object with many pieces, I had insert them as child of an empty gameobject for to disable them I disable the gameobject and not every sprite render's pieces
     
  6. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Can you post the relevant code, please?