Quantcast
Channel: Answers for "GameObject.Find() work on inactive objects"
Viewing all articles
Browse latest Browse all 45

Answer by bburtson09

$
0
0
You Are looking for Resources.FindObjectsOfTypeAll(), as Noob_vulcan said. I wrote some test code and found many solutions, I will share 2 of my solutions the first being a broad implementation that is flexible enough for most projects. I am using the System.Linq namespace in these examples. Snippet 1: var allObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject)); var inactiveObjects = allObjects.Select(p => p as GameObject) .Where(h => h.activeInHierarchy == true); foreach (var inactive in inactiveObjects) { print(inactive.name); } This snippet will return a collection of all inactive Game Objects in the scene and if you try this code you will see that your console output will display a few objects under the hood you didn't know were there. Snippet 2: var allObjects = Resources.FindObjectsOfTypeAll(typeof(GameObject)); //if you want to find it by your name similar to GameObject.Find("myGameObjectName") var myObject = allObjects.Select(p => p as GameObject) .Where(n => n.name == "myGoName").SingleOrDefault(); //use your gameObjectsName name for your filter predicate print(myObject.name); myObject.SetActive(true); This code will return a single gameObject that matches your predicate string you test against:: in this case "myGoName". When I ran this code I had a tangible reference to my game object that I was able to set back to active which I'm sure means you can manipulate its components and values any way you like. You might be thinking "this seems suspiciously inefficient", but I want to just say quickly that linq uses something called deferred execution, and if used properly is tremendously efficient. I will not get into the details of the system.linq namespace because it's beyond the scope of this question. * and can definitely be more efficient than your boilerplate foreach loops. I hope this helps!

Viewing all articles
Browse latest Browse all 45

Trending Articles