- Home /
[c#]How to start a coroutine after deactivating and re-enabling script?
my script basicly does this(the object is located in the start and variables are there, i just didnt copy them):
if(Input.GetKey("-")){
Spawner.GetComponent<VSM20>().enabled = false;
}
if(Input.GetKey("=")){
Spawner.GetComponent<VSM20>().enabled = true;
}
they both work and when the count is there(button for testing purpose), no more cars are spawning. the reference is working i guess, the script is activated and deactivated using the correct button. but the coroutine is not starting again once the script is deactivated.
i tried putting the StartCoroutine(MyCoroutine()); in the start() section and in the void Awake() section and both change nothing.
i also tried leaving the script on and having a remote bool enable command(working also) this was working also but when i put in the coroutine the if enabled==true, it just kept spawning cars when the value was false(debug.log showed me)
this is how the routine looks like:
IEnumerator MyCoroutine() { SecondsPre=Random.Range(2.0f,4.0f); Seconds=Random.Range(5.0f,10.0f); yield return new WaitForSeconds(SecondsPre); Instantiate(spawning, transform.position, Quaternion.identity); CurrentCars=PlayerPrefs.GetInt("CurrentCars"); CurrentCars++; PlayerPrefs.SetInt("CurrentCars",CurrentCars); yield return new WaitForSeconds(Seconds);
}
so im not sure how to get the coroutine started for a second time. i believe i had this issue before using a respawn with a timer and it also didnt respawn me after it respawned me for one time(so the second respawn failed, even if i putted if 1>0 statement xD so i just gave up on that one.
Your answer
Follow this Question
Related Questions
Enable and disabled parent contraint with c# 1 Answer
Enable/Disable GameObject Button script 1 Answer
Can't remove Rigidbody from transform in script? 8 Answers
Disable gameobject at other screen with code ? 0 Answers
Unable to enable script 0 Answers