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.

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.

Try something like this

   yield return StartCoroutine(EnemyContactTimer());

The OrganiseChildren should pause untill EnemyContactTimer is finished