Crash when using GameObject.Find

Hi,

I'm currently having issues using the GameObject.Find(String) method.

My game is sending a message to my server thanks to a GameObject and once the server is answering to me, I'm analyzing its response in another GameObject.

Here is the method sending the message to the server in my GameObject XMLParser :

function Awake() {
    FindObjectOfType(NetworkRedDwarf).Send("getArtefacts"); // this method works fine
}

And here is the method called to analyze the answer in my GameObject NetworkRedDwarf :

``` public void analyzer(string commande, string args) { GameObject xmlParser = GameObject.Find("XMLParser"); // my game crashes here xmlParser.SendMessage("build", args); } }

And here the error given by the console :

UnityEngine.GameObject:Find(String)
NetworkRedDwarf:analyzer(String, String) (at Assets\Standard Assets\Network\NetworkRedDwarf.cs:73)
```

I'm still looking for a solution. Any idea on what's going on ? Is it because my XMLParser GameObject isn't totally built (I'm using the Awake method) ?

Any help would be appreciated.

Thanks !

2 Answers

2

Thanks to "Mike", I've been able to solve my problem (in case someone would be stuck in the same way).

Unity isn't threadsafe, so using most of the API from a thread will cause it to crash, throw exceptions or just do weird stuff

What you generally need to do is put your results into a queue of some sort and parse them from Update, or another unity synchronized function

The console error is just a stacktrace to where the error occurred, which you knew already. :)

According to http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Awake.html, you should be able to query objects there, so as long as there is a GameObject called XMLParser, you should be fine. Notice however, that the article states you should use Awake to set up references, but that you should use Start to pass any information back and forth. The way I understand that is that your call to GameObject.Find should be fine, but the call to SendMessage might not be.

Thanks for your reply. I checked my scene by pausing the game and my GameObject called XMLParser seems to be present (but I paused it after the crash, maybe juste before the call to Find, my GameObject is not present). The game crashes if I don't use the method SendMessage and even if I try to locate another GameObject (not matter which one). That's weird.