- Home /
How to find Inactive GameObject
I Have number of objects within the scene that conform to the naming convention:
male_portrait_
Where n is the number of that piece of portrait geometry. When the player clicks on a collider around the portraits, I am activating the next portrait and deactivating the current.
Given that only one can be shown at a time, I've been deactivating all but the first and then in code I was using GameObject.Find("/male_portrait_"), but I was surprised to find that this did not find inactive objects.
I was wondering if there was a way of finding inactive objects?
NOTE: It occurs to me now that I could have turned the mesh renderer and toggle this state in code as the player clicks and still be able to find the objects as I tried to above.
NOTE: my solution was to fix my code so that the objects were all active and I then deactivated the ones that should be hidden.
I'll post solution(s) separately.
This question hardly needed any body text at all. Title is quite self explanatory and current body gives almost no extra information! :P
Answer by Bovine · Feb 19, 2011 at 11:48 PM
Okay, so I have two solutions:
Solution 1 I left everything active, discovered all the items programatically and turned off the ones I didn't need.
Solution 2 I could hide the mesh renderer in the editor and discover the objects as above.
The confusing thing for me is that within the editor I can drag an inactive object onto the member variable of another script, thereby linking that inactive instance to that script, but I cannot do the same programmatically.
Honestly, this is not even an actual answer (to the title, at least)! Please, see my answer there (or here, in case the previous link doesn't work).
you can find more here http://www.unityrealm.com/how-to-find-inactive-gameobject-in-unity/
LOL yea it get on my nerve every time x'D u must make a reference and assign it from the inspector , damn! but i like ur idea (Solution 1) (Y)
Answer by Simone · Feb 22, 2011 at 01:15 PM
I agree it is rather confusing. I expect I can FIND an inactive GameObject
Answer by cregox · Sep 19, 2012 at 04:04 PM
Recursive for the win!
The idea here is using either a non-ideal way of getting all root `GameObjects` or Tagged objects, as a place where to begin finding. Specially on the first way, this should be slower than regular `GameObject.Find`, but it will go through inactive objects.
static public GameObject GameObjectHardFind (string str) {
GameObject result = null;
foreach ( GameObject root in GameObject.FindObjectsOfType(typeof(Transform)) ) {
if (root.transform.parent == null) { // means it's a root GO
result = GameObjectHardFind(root, str, 0);
if (result != null) break;
}
}
return result;
}
static public GameObject GameObjectHardFind (string str, string tag) {
GameObject result = null;
foreach ( GameObject parent in GameObject.FindGameObjectsWithTag(tag) ) {
result = GameObjectHardFind(parent, str, 0);
if (result != null) break;
}
return result;
}
static private GameObject GameObjectHardFind (GameObject item, string str, int index) {
if (index == 0 && item.name == str) return item;
if (index < item.transform.childCount) {
GameObject result = GameObjectHardFind(item.transform.GetChild(index).gameObject, str, 0);
if (result == null) {
return GameObjectHardFind(item, str, ++index);
} else {
return result;
}
}
return null;
}
Keep in mind this is compatible with Unity 4 (just tested it on beta 7) but should still be avoided and it is even more unnecessary, due to how U4 now handles active and inactive game objects.
Answer by doomprodigy · Feb 18, 2011 at 01:29 PM
If I get what your saying. You want to disable a object and enable the rest. use SetActiveRecursively if true it will enable the item/activate and that object children aswell. if false it will deactive the object and it's children.
If you want to only disable one part of the object use active.
Heres an example. C#
public GameObject currentlyenabled = GameObject.FindWithTag("enabled"); public GameObject currentlydisabled = GameObject.FindWithTag("disabled");
//After your collision script enter this currentlyenabled.SetActiveRecursively(false); currentlydisabled.SetActiveRecursibely(true);
JavaScript
var currentlyenabled = GameObject.FindWithTag ("enabled"); var currentlydisabled = GameObject.FindWithTag ("disabled");
//After your collision script enter this currentlyenabled.SetActiveRecursively(false); currentlydisabled.SetActiveRecursibely(true);
Using the tags keep it a bit tidy compared to having multiple game objects going on and off just tag the one enabled with "enabled" and all the ones that are disabled currently "disabled".
If your asking how you can find and turn something on/off that should work. If your asking to find something inactive and access scripts from I am fairly sure you cannot do that, but what I would do in that case is turn that objects render off.
I am a bit stumped on what your asking.
Peace,
EDIT: Did you test it with the tag method that I posted before instead of just using Gameobject.Find ? If that does not work another method would be to make a Gameobject variabled in which you then manually put what object it is into the inspector and then SetActiveRecursively it (this will work).
Example C#:
public GameObject male_portrait;
male_portrait.SetActiveRecursively(true);
Example Java:
var male_portrait : Gameobject;
male_portrait.SetActiveRecursively(true);
Thanks for the detailed answer - I think my description might need clarifying. I am using SetActiveRecursively() but the problem is not activating or deactivating... I'll edit my question.
I've solved this by having the items active at start and deactivating those that should be hidden.
Answer by Owen-Reynolds · Sep 19, 2012 at 08:39 PM
Can set up an array at the start with links to them all. Then you never need to run a `Find` while running, so no worries about inactive. That also puts all the naming stuff in one place.
public GameObject[] Portrait;
// maybe set size in the inspector and drag all in, or:
void initPortraits() {
Portrait = new GameObject[10];
string pBase = "male_portrait_";
for(int i-0;i<10;i++) {
Portrait[i] = GameObject.Find(pBase+i); // Ex: male_portrait_0
Portrait[i].active=false;
}
}
The regular code can now say things like: `Portrait[curPort].active=false; curPort++; Portrait[curPort].active=true;`
This is actually a good idea, and most of the times a better way to think. It's also basically how he already "solved" the issue (making all objects active to begin with). But those methods don't address directly the point made in question's title.
Your answer
Follow this Question
Related Questions
GameObject.Find() work on inactive objects 16 Answers
Toggling a game object between an active and inactive state 1 Answer
Activate gameobject that is deactivated with c# 2 Answers
Best way to keep track of inactive GameObjects? 1 Answer
Organize around inactive GameObjects not being findable 0 Answers