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