- Home /
When I try to enable a GameObject that's already disabled, the GameObject is null and can't be enabled via C# code
Hello community.
When I try to search a disabled GameObject the same is null with the method
GameObject.Find("GameObjectNameToFind");
But if I make a public property of GameObject and I assign it via the editor, then the GameObject isn't null.
Public GameObject gameObjectToBeAssigned;
It's a bug or there's a different way to work with disabled GameObject via code?
I'm working with .NET 4.6 experimental and C# as you can see.
Regards.
Answer by Crazydadz · Dec 17, 2017 at 04:23 AM
This function only returns active GameObjects. If no GameObject with name can be found, null is returned. If name contains a '/' character, it traverses the hierarchy like a path name.
This is not a bug but by design.
https://docs.unity3d.com/ScriptReference/GameObject.Find.html
There's a method to found a GameObject disabled? Or I must assign the GameObject via editor with a public property like I've indicated?
Thanks for the explanation.
Answer by Albarnie · Dec 17, 2017 at 06:28 AM
GameObject.Find only returns enabled objects. establish a reference to the object, then enable it. As a general rule, never use GameObject.Find. it is slow and processer intensive.
I use it on the start method to get the GameObject on the start, there's no problem if I use it that way, doesn't it? If there is, what do you recommend?
Your answer
Follow this Question
Related Questions
How do I make do I make a variable go back to its original value for a spell system 1 Answer
Having trouble using SendMessage 1 Answer
3rd Person Camera to Rotate With the Player 0 Answers
How to use method of other script of different unityPackage that i import? 1 Answer
How do I make my projectile deal damage to a target it hit. 1 Answer