instantiate not working after Application.LoadLevel

hi, all,

in my game, the user can add objects at runtime. i have a run button to start time running, and i save the dynamically added objects to list just prior to starting time

when the user hits stop, i want to reload the original scene and the dynamic objects from the list, so i have this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RunControl : MonoBehaviour {

    private bool isRunning = false;

    public List<GameObject> InstantiatedObjects;    // save instantiated objects when user hits run, reload them, after a stop

    void Start() {
        Time.timeScale = 0.0f;
    }

    public void OnClickRun () {
        //Debug.Log ("OnClickRun");
        if (isRunning) {
            // set to pause
            Time.timeScale = 0.0f;
            isRunning = false;
        } else {
            // set to run
            Time.timeScale = 1.0f;
            isRunning = true;
        }

        Gizmo.globalClearSelection = true;

        foreach (GameObject obj in GameObject.FindObjectsOfType(typeof(GameObject))) {
            //Debug.Log ("OnClickRun obj=" + obj.name);
            if ( obj.name.IndexOf("(Clone)") != -1 ) {
                Debug.Log ("OnClickRun obj=" + obj.name);
                InstantiatedObjects.Add (obj);
            }
        }
        Debug.Log ("OnClickRun added " + InstantiatedObjects.Count);
    }
   
    public void OnClickStop () {
        Time.timeScale = 0.0f;
        Application.LoadLevel(Application.loadedLevel);
        //SceneManager.LoadScene ();

        foreach (GameObject obj in InstantiatedObjects) {
            Debug.Log ("OnClickStop obj=" + obj.name);
            Instantiate (obj);
        }
        Debug.Log ("OnClickStop added " + InstantiatedObjects.Count);

    }
}

all looks ok, except the Instantiate (obj);doesn’t do anything, no new objects show up in the scene.

i know Instantiate works because i use it in other places ok, so why not now?

thanks

Hi

Two things.

1:
Is your MonoBehaviour an instance in the scene?
If it is then is it is removed when doing the LoadLevel.

2:
LoadLevel (LoadScene) does not complete immediately so what actually happens is:

  1. You call LoadLevel which puts the load on a queue
  2. you instantiate the object in your current scene
  3. Your load is actually executed and everything, including your instantiated objects, are unloaded

You need to make sure your MonoBehaviour is not destroyed and you need to make sure you don’t instantiate the new objects in a scene which is going to be unloaded immediately afterwards

I would use the SceneManager to load scenes additively and have a “Manager” scene always present where things that needs to persist across scene loading is put. Use SceneManager.sceneLoaded, to know when scene loading have completed, so you can instantiate your objects. Another way is to use DontDestroyOnLoad.

hi steenlund, yes, that seems to be the problem, so i did this which is ugly but works

using UnityEngine;
...
using UnityEngine.SceneManagement;

public class RunControl : MonoBehaviour {

private static int reloadUserObjectsCountdown=-1;

public void OnClickStop ()
    {
        Time.timeScale = 0.0f;

        SceneManager.LoadScene (SceneManager.GetActiveScene ().name); //Application.loadedLevel);

        reloadUserObjectsCountdown=10;    // this is f'ed, have to wait a few frames before can reload user objects after SceneManager.LoadScen
    }
    void Update()
    {
        if (reloadUserObjectsCountdown == -1)
            return;
      
        reloadUserObjectsCountdown--;
        Debug.Log ("OnClickStop reloadUserObjectsCountdown = " + reloadUserObjectsCountdown);
        if(reloadUserObjectsCountdown==0) {
          
            GameObject basePrefab = (GameObject)Resources.Load ("Domino", typeof(GameObject));
            Instantiate (basePrefab, new Vector3 (0, 0, 0), Quaternion.identity);

        }
    }


}

i tried a coroutine method with a yield return new WaitForSeconds(waitTime); but for some reason it never returned, but now i think about it, i think its because the IEnumerator was not static so it was getting destroyed on loadscene