Code stops running at Instantiate but no errors

Hi,

I’m having trouble understanding why my first log message below prints, but never the second one. There are no errors, but the object never instantiates. I’ve tried stepping through the code using the VS debugger and it simply returns to the Unity scene after the Instantiate line. Any ideas?

protected GameObject currentPreview = null;

  protected void InstantiatePreview(GameObject prefab) {
        if (currentPreview != null) { Destroy(currentPreview); }
        Debug.Log("Instantiating preview prefab");
        currentPreview = Instantiate(prefab, PreviewPosition, Quaternion.identity);
        Debug.Log("Finished instantiating preview prefab");

So, this took me a while to figure out. I was instantiating outside the main thread, which Unity cannot do. I do wish it would throw a clear error message, though. Simply exiting the code is extremely frustrating to debug!

In case it’s relevant to anyone, the problem has to do with the fact that Unity cannot instantiate GameObjects if the method is not part of the main thread (essentially it should be called by Monobehaviour event methods like Update, Awake, OnMouseDown, etc.).

In my case, the method was being triggered by a Timer elapse event, which of course is not part of the Unity main thread events. I had to change the code so that instead of waiting for a timer to elapse, I manually checked for a certain time to pass inside the Update() function.