- Home /
SetActive not reactivating with Tags
Sorry for resurrecting an old Question http://answers.unity3d.com/questions/390990/gameobject-setactive-not-reactivating.html 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 @ http://answers.unity3d.com/questions/557612/does-setactive-work-with-components.html 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.
Answer by rutter · Oct 23, 2013 at 07:44 PM
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.
Thank you very very much. That does clarify quite a bit.
$$anonymous$$y code still is not working but I think it is due to the use of NGUI and the "NGUITools.SetActive(example1, true);"
NGUI uses a different SetActive do to the order in which unity turns things off and on.
I had fixed the variable vs function issue in my code and you answer clarified why it needed fixing. (I have also updated the code in my question to reflect the proper use of a function as you explained)
Testing the loop (Thanks for the clarification on this as well) makes sense but I still can't get the deactivated gameobject to turn back on.
I have spent most of my day reading the scripting reference but I don't feel they explained the SetActive very well. The old Recursive code people keep mentioning on the forums makes more sense but Im sure they had good reasons to change it.
Thanks, not yet answered but I'm giving a thumbs up.
O$$anonymous$$ So rutter's answer is completely right however when using NGUI the NGUITool script messes with the normal behavior of Unity (and with good reason)Basically Unity activates children before parents.
So you have to use
NGUITools.SetActiveChildren(example1, false); and NGUITools.SetActiveChildren(example1, true);
I asked on the NGUI Tasharen forum about this issue and was told about NGUITools.SetActive(targetGameObject, true/false)
But it wasn't until I added Children that I started seeing any progress.
Now I have:
using UnityEngine;
using System.Collections;
public class ObjectActivation : $$anonymous$$onoBehaviour {
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
NGUITools.SetActiveChildren(example1, false);
NGUITools.SetActiveChildren(example2, false);
NGUITools.SetActiveChildren(example2, false);
void Update () {
}
//Calls the click event of Vuforia
void On$$anonymous$$ouseDown () {
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":
NGUITools.SetActiveChildren(example1, true);
NGUITools.SetActiveChildren(example2, false);
NGUITools.SetActiveChildren(example2, false);
isClicked = false;
break;
//etc. etc.
}
}
else {
switch (obj_tag)
{
case "tag_one":
example1.gameObject.SetActive(false);
isClicked = false;
break;
//etc. etc.
}
}
}
}
As such I am marking rutters answer as correct
Answer by molton215456 · Dec 28, 2013 at 02:43 AM
GameObject example1 = GameObject.Find("example1");
foreach (Transform child in example1.transform)
{
child.gameObject.SetActive(true);
}
thanks rutter, that fixed my gameobject enabling/disabling problems
Your answer
Follow this Question
Related Questions
Unity 4 gameObject.active equivalent 2 Answers
Hi, getting error cs1041 in my code:identifier expected, "this" is a keyword; need solution please 3 Answers
How to swap gameObjects on the press of a button? 1 Answer
How do I alter all children of a gameobject at once? 1 Answer
Deleting gameobject with tag problem 1 Answer