MonoBehaviour transform is null

Hi All,

I have a bug in my game that I cannot reproduce to debug. I am hoping you all can help point me in a good direction.

I have one beta tester who keeps getting a null reference exception. The object in question is created at the start of the game, the object is a level marker. When the user completes a level I enable the stars on the map for the completed level. The user is gettting a null refreerance exception when I call transform.Find(“somePath”);. Is there any reason why the transfrom of a MonoBehaviour would become null?

Thank you,

Travis Pettry

It is a bad idea to use the Find() method as it will not give you a clear idea of where your game is breaking. A null is returned as it cannot find the object you want it to find. Have you checked the name of the object at runtime etc?

Also, take a look at the docs it says “Find does not perform a recursive descend down a Transform hierarchy.”

You should keep a track of objects at start time perhaps as a pool maybe.
Then you can always find the object you are looking for unless it’s destroyed.
However, if you are only creating or using a few objects just create an array of objects from some SpawnManager class for example.