[SOLVED] Coroutine Null reference

I’ve looked at all the similar cases and I’m afraid I don’t understand how others [SOLVED] this:

NullReferenceException: Object reference not set to an instance of an object
Value+d__8.MoveNext () (at Assets/Scripts/Value.cs:53)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Value : MonoBehaviour
{
    [SerializeField]
    private bool sizeMass;
    public int value;
    private Text displayNumber;
    [SerializeField]
    public GameObject underline;
    private int maxValue;
    [SerializeField]
    private int difficulty;
    private Rigidbody2D rb;
    // Start is called before the first frame update
    void Awake()
    {
        StartCoroutine(Assign());
    }
    private IEnumerator Assign() // creates a random value on a cargo piece, 
                        //based on needs of the current equation and difficulty
    {
        int colorMe = GetComponent<ColorMe>().leftOrRight;
        yield return new WaitForSeconds(.1f);
        if (colorMe == 2)
        {
            GameObject leftovers = GameObject.Find("Killzone Green");
            Restarter restarter = leftovers.GetComponent<Restarter>();
            maxValue = restarter.leftover + difficulty;
        }
        if (colorMe == 1)
        {
            GameObject leftovers = GameObject.Find("Killzone Pink");
            Restarter restarter = leftovers.GetComponent<Restarter>();
            maxValue = restarter.leftover + difficulty;
        }
        if (value == 0)
        {
            if (maxValue >= 11)
            { maxValue = 11; }
            if (maxValue <= 0)
            { maxValue = 1; }
            value = Random.Range(1, maxValue);
            if (value == 9 || value == 6)
            {
                underline.SetActive(true);
            }
        }
        displayNumber = transform.GetComponentInChildren<Text>();
        displayNumber.text = "" + value;
        if (sizeMass)
        {
            transform.localScale += new Vector3(value/7f, value/7f, 0);
            rb = GetComponent<Rigidbody2D>();
            rb.mass = rb.mass*value/3;
        }
    }
    private void Update()
    {
        if (transform.position.y < -30)
        {
            Destroy(gameObject);
        }
    }
}

The error is in:

            if (value == 9 || value == 6)
            {
                underline.SetActive(true);
            }

I have the underline assigned through inspector.

How to solve this :cry: ?

P.S. I know the code is horrible. It’s an old project and I’ve learned a lot since then.

Either there’s no Text component in that transform hierarchy or it is disabled

Are you sure that underline.SetActive(true); is at line 53?

1 Like

It can not be because variable can not be 0 and (6 or 9) at the same time

He resets value in line 46

It is disabled, yes. It is meant to be enabled in code.

Is this all I have to do, enable it first, disable it early on and then enable again when necessary?

Yes, the error points to that line, and that line is at #53, whatever the code may say ITT.

If you are sure that it is that line and underline is assigned in the inspector, check that you don’t have two instances of the class in the scene.

After doing this, the error switches to:

UnassignedReferenceException: The variable underline of Value has not been assigned.
You probably need to assign the underline variable of the Value script in the inspector.
Value.Awake () (at Assets/Scripts/Value.cs:23)
UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion)
spawn:Update() (at Assets/Scripts/spawn.cs:71)

Which is:

    void Awake()
    {
        underline.SetActive(false);  // <<< THIS
        StartCoroutine(Assign());
    }

and

        if (currentTime >= 0)
        {
            currentTime -= Time.deltaTime;
            cooldown.text = Math.Round(currentTime, 1).ToString();
        }
        else
        {
            which = UnityEngine.Random.Range(0, whatToSpawn.Length);
            whatISpawned = Instantiate(whatToSpawn[which], this.transform.position, this.transform.rotation); // <<< AND THIS
            spawnedBody = whatISpawned.GetComponent<Rigidbody2D>();
            if (spawnInterval >= minimumSpawnInterval)
            {
                spawnInterval += intervalIncrease;
            }
            else
            {
                spawnInterval = minimumSpawnInterval;
            }
            currentTime = spawnInterval;
            spawnedBody.AddForce(whatISpawned.transform.up * forwardThrust);
        }

(And of course, it has been assigned in the inspector because it also functions as intended).

Clearly, it isn’t assigned somewhere. Check that the value script isn’t on more than one game object.

Oh, I have many instances of the class. Usually I had no trouble having several gameobjects with the same class perform similar/same operations on objects with the same names. I need a singleton here? That’s odd…

Yes, indeed it’s on many game objects. On every single object that spawns as “cargo” … and they keep spawning all the time. But I actually think the last time I was testing (showing the game) to some friends, the errors were miraculously gone.

I will have to check and get back to you on this.

If this is the case, then the previous step would have solved it
(having it enabled first, then disabling/enabling later in code).

Sorry for being slow, life is a mess at the moment.

[SOLVED] >>>

The gameobject which was assigned in the inspector (Underline) was disabled at the Start/Awake. So if you want to enable an object in the scene, you need to first disable it at Awake, then enable it again later.

I didn’t expect this to be the case because in certain cases it wouldn’t be a very optimal solution.

You probably missed includeInactive argument in this man page: https://docs.unity3d.com/ScriptReference/GameObject.GetComponentInChildren.html

No, because I’m not getting a component in children, I’m providing it to the script through the inspector.