- Home /
GameObject.Find no longer working for specific classes
Hi all, I'm at my wits end. Don't expect an answer but just in case someone else has had this experience...
I'm an experienced Unity coder. My project makes use of GameObject.Find (in C#) -- not a big surprise there. It worked fine until yesterday afternoon.
Then, I built a prototype version for the Web Player so my client could view it. I came back after a half hour break, made a small code change (added a single class and attached it to an object), and when I ran it, I had about a dozen NullReferenceException errors.
At first I thought it was the component scripts that had somehow gotten unattached to their objects. But I have narrowed it down to two scripts where GameObject.Find is failing to find an object that every other script in the project is Find'ing without a problem.
I won't post code because there's nothing to post, just a few GameObject.Find's in different classes.
Anyone else had this experience after building for the Web Player? I am totally baffled.
Answer by DaveA · Feb 07, 2013 at 05:27 PM
GameObject.Find does not find classes, it finds objects by name. Maybe you meant http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectsOfType.html ?
Also make sure the objects are active (no scripts turning them off?) Find won't find inactive objects.
Thanks Dave. The GameObject.Find() is in fact failing to find a game object (empty in my case, but a GameObject nonetheless).
Your answer did help me remember there are other ways to get at the object I need. Hopefully FindByTag or FindObjectsOfType aren't worthless in this case... will give those a try.
Turned out that adding a tag to the object and then using GameObject.FindGameObjectByTag() worked. Still doesn't make sense, but it works! Thanks for pointing me in the right direction.