SetActive not reactivating with Tags

Sorry for resurrecting an old Question GameObject SetActive not reactivating - Unity Answers but this still does not make sense to me and I didn’t want to highjack that thread.

I am not using the keyboard to activate or deactivate but rather onClick or onMouseDownof one gameObject to SetActive another game object.

For Example:

using UnityEngine;
using System.Collections;
 
public class ObjectActivation : MonoBehaviour {

    GameObject example1;
	GameObject example2;
	GameObject example3;
    string obj_name;
	string layerName;

 void Start () {
    example1          = GameObject.Find( "item1" );
	example2          = GameObject.Find( "item2" );
	example3          = GameObject.Find( "item3" );

        //NGUITools.SetActive(example1, false); //if using NGUI
          example1.gameObject.SetActive(false);
          example2.gameObject.SetActive(false);
          example3.gameObject.SetActive(false);

 void Update () {
     
    }
	//Calls the click event of Vuforia
	void OnMouseDown () {
	    obj_name            = this.gameObject.name;
        baseObject          = GameObject.Find( obj_name );
        string obj_tag      = baseObject.gameObject.tag;  

		
        if (isClicked == false){
			switch (obj_tag)
			{
                          case "tag_one": 
				example1.gameObject.SetActive(true);
                                example2.gameObject.SetActive(false);
                                example3.gameObject.SetActive(false);
				isClicked = false;
			  break;

                          case "tag_two": 
				example1.gameObject.SetActive(false);
                                example2.gameObject.SetActive(true);
                                example3.gameObject.SetActive(false);
				isClicked = false;
			  break;

                          case "tag_three": 
				example1.gameObject.SetActive(false);
                                example2.gameObject.SetActive(false);
                                example3.gameObject.SetActive(true);
				isClicked = false;
			  break;
                  }		             
	       }
	    else {
	        switch (obj_tag)
			{
			 case "tag_one":
                               example1.gameObject.SetActive(false);
			       isClicked = false;
			  break; 

			 case "tag_two":
                               example2.gameObject.SetActive(false);
			       isClicked = false;
			  break; 

			 case "tag_three":
                               example3.gameObject.SetActive(false);
			       isClicked = false;
			  break; 
			}    		
	    }
    }
}

Note: The above code is just a rough example I have not tested it, however my code is very similar with added Hide(“layerName”); and ObjectName.Camera.enabled = true; and other such features.

I can Set Active to false but how do I set Active to true? Since I am clicking one object to activate another the “foreach (Transform child in this.transform)” wont work as it is not “this” that I want active and the getComponentsInChildren is incorrect as I want to activate everything not just the components. A component solution can be seen @ Does .SetActive work with components? - Unity Answers
However that is not solving my issue.

I have tried coroutines (I think this is a coroutine):

GameObject[] i = GameObject.Find("example1").SetActive();
foreach(gameObject e in i)
e.gameObject.SetActive(true);

(or is that a loop? I’m fairly new to code)

However I cant seem to figure out how this works. All the documentation says I should just use SetActive(true) to reactivate my deactivated game objects but that does not work.

I’m sure this is simply me being stupid and not understanding something in the code but I have spent two days reading about SetActive and I still can not get it to reactivate.

First off, good on you trying to learn programming! It’s a difficult path, but there are quite a few resources and tutorials around the net to help you out. Finding good reading material is hard, but almost always worth the effort.

So…

It’s important to distinguish between variables and functions.

//variable
//declare an integer value "x", assign a value of 5
int x = 5;

//function
//call the function "MyFunction" with a value of 5
MyFunction(5);

//both at once
int x = 5;
MyFunction(x);

So this line, and others like it:

example1.gameObject.SetActive = false;

Make no sense to the compiler, because SetActive is a function. You can instead do something like this:

example1.gameObject.SetActive(false);

The parentheses indicate a function call.

Now, Unity has another feature: GameObjects can be children of other GameObjects, allowing you to organize your scene however you like. If a GameObject’s parent moves, it moves with it; if its parent becomes inactive, it becomes inactive with it.

SetActive() will set the activeSelf flag for any single GameObject. If it’s inactive, its children will also be inactive. If it’s active, its children might be active depending on their own activeSelf booleans.

Finally, we come to your foreach loop (and yes, that is a loop):

GameObject[] i = GameObject.Find("example1").SetActive();
foreach(gameObject e in i)
e.gameObject.SetActive(true);

Here, you’re trying to take the return value from SetActive() and assign it to the variable i. There are a few reasons that won’t work:

  • SetActive() doesn’t return an array of GameObjects.
  • SetActive() needs an argument (a bool, in this case).

What’s that loop actually trying to do? Activate all of example1’s children? That would look something like this:

GameObject example1 = GameObject.Find("example1");
foreach (Transform child in example1.transform)
{
    child.gameObject.SetActive(true);
}

I hope that answers a few of your questions. Really, though, you’re going to need to spend a lot of time reading and following tutorials. The scripting reference is a great study guide for Unity-specific questions, but a basic understanding of programming is still a great help.

GameObject example1 = GameObject.Find(“example1”);
foreach (Transform child in example1.transform)
{
child.gameObject.SetActive(true);
}
thanks rutter, that fixed my gameobject enabling/disabling problems