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.