- Home /
NullReference exception after StopCoroutine
This is a little bit of a mess so I hope I can explain it well enough.
In my code I have a Coroutine called OrganiseChildren which moves around the children of the game object depending on what's going on in game. This function works fine.
However, if the game object hits another object then I'd like there to be a 'grace' period of 3 seconds in which everything stops moving. If the object gets hit again during the grace period, then it should start back at 3 and so on. My strategy for this is to write a separate coroutine (EnemyContactTimer) that, if the player is hit, stops the OrganiseChildren coroutine, waits until the time has passed, and then starts it again.
Here's the relevant code:
private float endTime;
IEnumerator organiseCoroutine;
void Start () {
endTime = -1;
organiseCoroutine = OrganiseChildren();
StartCoroutine(organiseCoroutine);
}
void OnTriggerEnter2D(Collider2D other)
{
endTime = Time.time + 3;
StartCoroutine(EnemyContactTimer());
}
}
IEnumerator EnemyContactTimer()
{
StopCoroutine(organiseCoroutine);
while(Time.time < endTime)
{
yield return null;
}
StartCoroutine(organiseCoroutine);
yield return null;
}
protected IEnumerator OrganiseChildren()
{
while (true)
{
//bunch of other code
}
yield return null;
}
}
The problem is that once the StopCoroutine function is called within EnemyContactTimer() it pulls a NullReferenceException at coroutine stops.
NullReferenceException: (null)
UnityEngine.MonoBehaviour.StopCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:86)
BallController+<EnemyContactTimer>c__Iterator1.MoveNext () (at Assets/Scripts/BallController.cs:53)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
BallController:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/BallController.cs:47)
Line 53 is the StopCoroutine(organiseChildren); line in the EnemyContactTimer function.
I'm completely stumped with this one and don't know why stopping the OrganiseChildren coroutine is causing a null reference in EnemyContactTime.
Any help with this would be much appreciated.
IEnumerator will throw error if you yield null; But in while block. They not throw any error.
IEnumerator EnemyContactTimer()
{
StopCoroutine(organiseCoroutine);
while(Time.time < endTime)
{
yield return null;
}
StartCoroutine(organiseCoroutine);
yield return null; < Try change this to - yield break;
}
Nope, didn't help. Thanks for the effort anyway.
I always got errors stopping them, so I never really used the StopCoroutine function. I do not know if this is exactly what you want, but here is a way you could just full out avoid stopping the coroutine. Ins$$anonymous$$d basically making it a toggle, so that until the coroutine is complete, do not allow it to be called again.
private float endTime;
private bool organizeStarted;
IEnumerator organiseCoroutine;
void Start () {
endTime = -1;
organizeStarted = true;
organiseCoroutine = OrganiseChildren();
StartCoroutine(organiseCoroutine);
}
void OnTriggerEnter2D(Collider2D other)
{
endTime = Time.time + 3;
if(!organizeStarted)
{
// Do not allow coroutine to be called until finished
organizeStarted = true;
StartCoroutine(organiseCoroutine);
}
}
protected IEnumerator OrganiseChildren()
{
// Wait for allotted time
while (Time.time < endTime)
{
yield return null
}
//bunch of other code
// Allow coroutine to be called again
organizeStarted = false;
}
I hope this helps.
Answer by MiniFish · Sep 30, 2015 at 10:19 AM
try this
IEnumerator EnemyContactTimer()
{
StopCoroutine(organiseCoroutine);
organiseCoroutine = OrganiseChildren(); // reassign the IEnumerator variable
while(Time.time < endTime)
{
yield return null;
}
StartCoroutine(organiseCoroutine);
yield return null;
}
somehow StopCoroutine() will cause the IEnumerator to clear itself. or rather, from my experiences, i cannot get the IEnumerator to start again if i stopped it.
i also suspect that you'd lose whatever values that is still inside the stopped coroutine when you stop it, so it's more like a restart than a pause and resume.
see the answer posted in this question. it should clear up a fair bit of coroutine magic.
Answer by Hullu · Sep 30, 2015 at 01:42 PM
Try something like this
yield return StartCoroutine(EnemyContactTimer());
The OrganiseChildren should pause untill EnemyContactTimer is finished