- Home /
Coroutine will be executed only once, why?
Hello,
I have a script that enables/disables rooms. When the room is enabled animation plays and the door of the prefab opens, using this script:
private Animator animator;
bool doorOpen = true;
private void Awake()
{
animator = GetComponent<Animator> ();
}
public void OpenDoor()
{
StartCoroutine(Open());
}
IEnumerator Open()
{
if (doorOpen)
{
yield return new WaitUntil(() => animator.isInitialized);
animator.SetBool("Open", true);
}
}
The script is called from another script that manages the room changing, when the "OpenDoor()" function is called the first time, the animation plays, however when the function is being called again in the next rooms, it doesn't execute it.
I've debugged each method and the problem is that the Coroutine won't start again, the "OpenDoor()" get's called but won't go to the coroutine "Open()".
EDIT: quick update it will show an error
Coroutine couldn't be started because the the game object 'Elevator' is inactive!
But why? I have an instance of this prefab at each room, so why can't it realize that when one instance in disabled the other is enabled and move on... why is it stuck on the disabled instance?
Answer by merkaba48 · Jul 06, 2017 at 11:39 AM
Do you have this script running on each door instance you have? If not, then you'd need to manually update the door instance reference to point to whichever door you want to affect next.
If you do have many instances of this script, each referring to their own door instance...then I don't know.
Your answer
Follow this Question
Related Questions
Coroutine a function within a loop? 1 Answer
AI function/coroutine animation issues 2 Answers
Coroutoutine doesnt work properly. 3 Answers
AnimationEvent has no function name specified 2 Answers
CORoutine does not completely stop 1 Answer