- Home /
The question is answered, right answer was accepted
Simple Coroutine doesn't finish
Hey! I am trying to create a mana-pickup item. Which will dissapear if a player touches it and then appear and the same place 2 seconds later.
When I run the code, everything seems to work, except it doesn't finish it: the component gets diabled and disappears, but doesn't appear back in 2 seconds, NOR does it print anything. Could you please help? Thank you very much!
Here's the code: using UnityEngine; using System.Collections;
public class manaboxscript : MonoBehaviour {
public GameObject enteredgameobject;
public imagescript linkvariable;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
enteredgameobject = other.gameObject;
linkvariable = enteredgameobject.GetComponent<imagescript> ();
StartCoroutine(WaitAndPrint(2.0F));
}
IEnumerator WaitAndPrint(float waitTime) {
gameObject.SetActive (false);
yield return new WaitForSeconds(waitTime);
print(waitTime);
print("WaitAndPrint " + Time.time);
gameObject.SetActive (true);
}
}
Or hand the job of waiting and then turning this object on again, to a different object.
both comments helped: I understood that coroutine ends when object is disabled and understood a work-around: to disable render and collision.
Answer by Bonfire-Boy · Feb 17, 2015 at 03:04 PM
When you deactivate an object, its coroutines stop.
Im agreed with Bonfire Boy, and then u must just turn off render and collision, then turn on it again
Follow this Question
Related Questions
Is there a yield WaitForSeconds type code for the update? 2 Answers
What is the best approach? Coroutine or calculate the "Elapsed Time" in the Update? 3 Answers
Confused about using Update vs. Coroutines 2 Answers
How can I get my camera to momentarily pause between different positions when using lerp? 0 Answers
Im a bit confused on a simple script 1 Answer