Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can FindGameObjectsWithTag find unactive game objects

Discussion in 'Scripting' started by malnar1996, Jan 6, 2011.

  1. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Hello, I have this script that changes my camera. In my team member have a big pointer above their body. So do the enemy team. I want to hide those pointers when camera1 is enabled and show them when camera2 is active. All pointers have a tag "pointer".

    Here is my script:

    Code (csharp):
    1. var camera1:Camera;
    2. var camera2:Camera;
    3.  
    4. function Start() {
    5.     var pointers = GameObject.FindGameObjectsWithTag ("pointer");
    6.     for (var pointer in pointers) {
    7.     pointer.active = false;
    8.     }
    9. }
    10.  
    11. function Update () {
    12. if (Input.GetButtonDown("Map")) {
    13.     if (camera1.enabled == true) {
    14.    
    15.     pointers = GameObject.FindGameObjectsWithTag ("pointer");
    16.     for (var pointer in pointers) {
    17.     pointer.active = true;
    18.     }
    19.    
    20.     camera1.enabled = false;
    21.     camera2.enabled = true;
    22.     }
    23.     else if (camera2.enabled == true) {
    24.    
    25.     pointers = GameObject.FindGameObjectsWithTag ("pointer");
    26.     for (var pointer in pointers) {
    27.     pointer.active = false;
    28.     }
    29.    
    30.     camera2.enabled = false;
    31.     camera1.enabled = true;
    32.     }
    33. }
    34.  
    35. }
    Camera change works fine but pointers get unactive in the start and they don't get active when I the camera change. Can you please tell me if FindGameObjectsWithTag finds unactive game objects or is there an easier way to do this?

    Thank you.
     
  2. ravinder

    ravinder

    Joined:
    Aug 25, 2010
    Posts:
    150
    Hello,

    I went through your code and noticed that you are taking reference to the pointers array 2 times:

    1. inside the Start()
    2. inside the Update()

    Infact 3times.
    Anyways don't take the references again and again just take them for only once. You can either take them inside Awake() or Start()
    and declare the pointers array globally outside like:
    Code (csharp):
    1.  
    2. var pointers: GameObject[];
    3.  
    4. function Start()
    5. {
    6.    pointers=GameObject.FindGameObjectsWithTag("pointer");
    7. }
    8.  
    Now by the above way you have all the references of pointer objects in your pointers array. Now you can easily activate and deactivate your objects whenever and wherever you want. It also increases your game performance.
    And one more thing FindTag does not find inactive objects present in your scene.

    Hope it helps!!:)
     
  3. malnar1996

    malnar1996

    Joined:
    Oct 31, 2010
    Posts:
    31
    Thank you. That globally thing helped me with my other codes but here, I have to FindGameObjectsWithTag every frame and I can't find them if they're unactive (when I wrote "pointer.active = false;" I made it unactive so I can't find it the other time I press button "Map").

    Thank you for your reply but I've done this by translating pointers below the terrain so the camera can't see them. It's not the best solution but it works.
     
  4. ravinder

    ravinder

    Joined:
    Aug 25, 2010
    Posts:
    150
    The alternative solution that you posted is also useful, even i also do the same way sometimes in case of prefab generation.
    One thing i am not understanding why do you want to find them in every frame when you already have their references stored once.
    Do you want them to get activated at different instances individually?
     
  5. Rapskalian

    Rapskalian

    Joined:
    Jan 4, 2011
    Posts:
    45
    Not sure if you fixed this issue yet but just an idea: instead of completely deactivating them you could just stop rendering them.

    Code (csharp):
    1. pointers = GameObject.FindGameObjectsWithTag ("pointer");
    2. for (var pointer in pointers) {
    3. pointer.renderer.enabled = false;
    4. }
    And than when you switch back to the other camera just reset it to true.

    Hope this helps!