Hello everyone! In the script below, the game manager copies the starting objects after a restart, however it keep instantiating and not stopping even after the stopcorountine. Does anyone know why it keeps instantiating?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameManager: MonoBehaviour {
private GameObject[] s1v;
private Text obj1Text;
//private GameObject obj1Image;
private string lvl1Reqs;
private string s1vls;
private int s1vl;
private int lvl1Req;
private Vector3 sbpSpawn;
private Vector3 sbSpawn;
public GameObject sbppre;
public GameObject sbpre;
public GameObject sbp;
public GameObject sb;
public bool restarting;
public bool doner = false;
public float target = 3f;
// Use this for initialization
void Start () {
restarting = false;
sbpSpawn = sbp.transform.position;
sbSpawn = sb.transform.position;
}
// Update is called once per frame
void Update () {
lvl1Req = 2;
s1v = GameObject.FindGameObjectsWithTag("Copy");
s1vl = s1v.Length;
lvl1Reqs = lvl1Req.ToString ();
s1vls = s1vl.ToString();
//Get a reference to our image LevelImage by finding it by name.
//obj1Image = GameObject.Find("obj1i");
//Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
obj1Text = GameObject.Find("obj1t").GetComponent<Text>();
obj1Text.text = "Viruses Cloned: "+ s1vls + " / " + lvl1Reqs;
if (s1vl == lvl1Req) {
if (doner == false) {
StartCoroutine("Restart1", target);
//Debug.Log("Restarting");
}
else if (doner) {
StopCoroutine ("Restart1");
}
}
}
IEnumerator Restart1(float target) {
//Identify we are restarting for other GameObjects so they can destroy themselves
yield return new WaitForSeconds(target);
restarting = true;
yield return new WaitForSeconds(target);
restarting = false;
Instantiate (sbppre, sbpSpawn, Quaternion.identity);
Instantiate (sbpre, sbSpawn, Quaternion.identity);
doner = true;
}
}