How to fix the flag bool variable become null and never stop the coroutine?

The script is attached to some objects and in another script when checking distance, I’m starting the blinking and then should stop the blinking. but it’s never got inside the stop method because the variable blinkCoroutine is null.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlinkingObject : MonoBehaviour
{
    public float blinkInterval = 0.5f;
    public Color blinkColor = Color.red;

    private Renderer[] blinkRenderers = null;
    private Color[][] originalColors = null;
    private Coroutine blinkCoroutine = null;

    private void Start()
    {
        CollectRenderers(transform, ref blinkRenderers); // Get all renderers in children and store them in blinkRenderers
        CopyOriginalColors(); // Copy the original colors of the materials of the blinkRenderers
    }

    private void CollectRenderers(Transform parent, ref Renderer[] renderers)
    {
        // Collect renderers in children
        List<Renderer> rendererList = new List<Renderer>();
        CollectRenderersRecursive(parent, rendererList);
        renderers = rendererList.ToArray();
    }

    private void CollectRenderersRecursive(Transform parent, List<Renderer> renderers)
    {
        // Recursive function to collect renderers in children
        foreach (Transform child in parent)
        {
            Renderer renderer = child.GetComponent<Renderer>();
            if (renderer != null)
            {
                renderers.Add(renderer);
            }

            CollectRenderersRecursive(child, renderers);
        }
    }

    private void CopyOriginalColors()
    {
        // Copy the original colors of the materials of the blinkRenderers
        originalColors = new Color[blinkRenderers.Length][];
        for (int i = 0; i < blinkRenderers.Length; i++)
        {
            Material[] materials = blinkRenderers[i].materials;
            originalColors[i] = new Color[materials.Length];
            for (int j = 0; j < materials.Length; j++)
            {
                originalColors[i][j] = materials[j].color;
            }
        }
    }

    private IEnumerator BlinkCoroutine()
    {
        // Blink the materials of the children of the object together
        while (true)
        {
            SetColor(blinkColor);
            yield return new WaitForSeconds(blinkInterval / 2f);
            SetOriginalColors();
            yield return new WaitForSeconds(blinkInterval / 2f);
        }
    }

    private void SetColor(Color color)
    {
        // Set the color of all materials in blinkRenderers to color
        for (int i = 0; i < blinkRenderers.Length; i++)
        {
            Material[] materials = blinkRenderers[i].materials;
            for (int j = 0; j < materials.Length; j++)
            {
                materials[j].color = color;
            }
            blinkRenderers[i].materials = materials;
        }
    }

    private void SetOriginalColors()
    {
        // Set the color of all materials in blinkRenderers to their original colors
        for (int i = 0; i < blinkRenderers.Length; i++)
        {
            Material[] materials = blinkRenderers[i].materials;
            for (int j = 0; j < materials.Length; j++)
            {
                materials[j].color = originalColors[i][j];
            }
            blinkRenderers[i].materials = materials;
        }
    }

    public void StartBlinking()
    {
        if (blinkCoroutine == null)
        {
            blinkCoroutine = StartCoroutine(BlinkCoroutine());
        }
    }

    public void StopBlinking()
    {
        if (blinkCoroutine != null)
        {
            StopCoroutine(blinkCoroutine);
            blinkCoroutine = null;
            SetOriginalColors();
        }
    }
}

and using it in another script:

if (distanceToWaypoint <= distanceThreshold)
            {
                waypoints[currentWaypoint].GetComponent<BlinkingObject>().StartBlinking();
            }
            else
            {
                waypoints[currentWaypoint].GetComponent<BlinkingObject>().StopBlinking();
            }

I tried to debug and it’s getting inside the method StopBlinking in the BlinkingObject script but the variable blinkCoroutine is null even if it’s first starting the blinking in the method StartBlinking and I checked when it’s getting inside the StartBlinking after doing this line:

blinkCoroutine = StartCoroutine(BlinkCoroutine());

the variable blinkCoroutine is not null but it become null when it’s trying to stop in the StopBlinking method.

how to fix it?

Null ref errors.

  1. Find out what is null
  2. Find out why it’s null
  3. Fix it.

Start adding Debug.Log calls and see what values you are targeting. Are you sure you’re calling StopBlinking on the same object you started on? Check all those values!