# Unity keeps throwing up NullReferenceException error when my scripts say no errors

**URL:** <https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825>\
**Category:** Scripting\
**Tags:** Scripting, 2022.3-lts, Question\
**Created:** [July 28, 2025, 8:54am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825 "2025-07-28T08:54:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CorydoraSpartan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CorydoraSpartan](https://discussions.unity.com/u/CorydoraSpartan)\
**Post date:** [July 28, 2025, 8:54am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/1 "2025-07-28T08:54:30Z")

</div>

THE GOAL: I want to reference my bool enemyExists from my EnemySpawnmanager script in my EnemyController script.

THE ERROR:  
NullReferenceException: Object reference not set to an instance of an object  
EnemyController.Update () (at Assets/Scripts/EnemyController.cs:22)  
Unity keeps throwing this up every time I try run the game in unity, but no errors are present in the C# side of things (at least that’s what visual studio 2022 is telling me).

THE CODE:

Script 1 (the one with the bool I want to reference):

```auto
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawnManager : MonoBehaviour
{
    public GameObject Enemy1;
    private float spawnDelay = 3;
    public bool enemyExists;
    private bool didFirstEnemy;
    private float xSpawnPos;
    private float xSpawnPosRange = 15.0f;
    private float ySpawnPos = 6.9f;
    private float zSpawnPos = 12.0f;
    private Vector3 spawnPos;

    // Start is called before the first frame update
    void Start()
    {
        Invoke("SpawnEnemy", spawnDelay);
        enemyExists = true;
        didFirstEnemy = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (didFirstEnemy)
        {
            SpawnEnemy();
        }
    }

    void SpawnEnemy()
    {
        if (!enemyExists)
        {
            xSpawnPos = Random.Range(-xSpawnPosRange, xSpawnPosRange);
            spawnPos = new Vector3(xSpawnPos, ySpawnPos, zSpawnPos);
            Instantiate(Enemy1, spawnPos, Quaternion.identity);
            enemyExists = true;
        }
    }
}

```

Script 2: the script in which I’d like to reference my bool:

```auto
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    private float killDelay = 3f;
    private EnemySpawnManager spawnManager;
    

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {

        if (spawnManager.enemyExists)
        {
            Debug.Log("cumsock bool is working");
            spawnManager.enemyExists = false;
            Destroy(gameObject, killDelay);
        }
    }
}

```

Please help.

---

<div class="post-metadata">

**Author:** ![meredoth](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/meredoth/32/92819_2.png) [@meredoth](https://discussions.unity.com/u/meredoth)\
**Post date:** [July 28, 2025, 9:10am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/2 "2025-07-28T09:10:58Z")

</div>

There’s a difference between runtime errors and compile time errors, both are C# errors, there are no Unity or C# specific errors. There are some errors that may originate from the C++ side of Unity, but when it’s about your scripts, then its “Unity and C#” or none.

The compile-time errors, are errors that can be caught as you write your code, and will be reported at that time. For example syntax errors, or trying to use a method that doesn’t exist in an object.

Runtime errors, are errors that cannot be caught when you write your code, but can only happen when you run it, as is the case with the null reference exceptions.

When there’s a null reference exception there are two steps:

1. Find what is null, this is easy as the error tells you the exact line where it happened, so if you don’t write big complicated lines with a lot of references then it is easy to find which variable is null.
2. Find why it is null, for this there are two things you should look at: If you have initialized this variable before it is called, and if yes, then to check if it gets dereferenced somewhere (becomes null somewhere between the initialization and where you are using it).

In your case, the error points to `EnemyController.cs`, line 22. By looking at the script, in this line there is: `if (spawnManager.enemyExists)`, the only variable that can be null here is the `spawnManager` variable.

Now let’s find why it is null, you declare this variable in line 9 like this `private EnemySpawnManager spawnManager;`. Is it initialized anywhere? No. You need to initialize it with a value.

There are different ways to do that, in this case the simplest way is to use the [SerializeField](https://docs.unity3d.com/ScriptReference/SerializeField.html) attribute and drag and drop the game object that has the `EnemySpawnManager` script in the field that will appear in the editor.

This is pretty basic functionality for Unity, I suggest to look at some tutorials on how to create dependencies between Monobehaviors in Unity and how to use SerializeField, before continuing with your code.

---

<div class="post-metadata">

**Author:** ![CorydoraSpartan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CorydoraSpartan](https://discussions.unity.com/u/CorydoraSpartan)\
**Post date:** [July 28, 2025, 9:21am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/3 "2025-07-28T09:21:01Z")

</div>

Thanks so much! I was not expecting such great help on this.

---

<div class="post-metadata">

**Author:** ![PhiMue](https://avatars.discourse-cdn.com/v4/letter/p/41988e/32.png) [@PhiMue](https://discussions.unity.com/u/PhiMue)\
**Post date:** [July 28, 2025, 9:58am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/4 "2025-07-28T09:58:13Z")

</div>

Be aware that [SerializeField] only works with objects that are already placed in the scene, or are part of the same prefab. If the object is instantiated at runtime, the spawnManager variable also has to be assigned at runtime. There are again a number of ways to do this, for example with FindObjectOfType (deprecated in Unity 6)/ FindAnyObjectByType. Or make the EnemySpawnManager a [Singleton](https://gamedevbeginner.com/singletons-in-unity-the-right-way/).

---

<div class="post-metadata">

**Author:** ![CorydoraSpartan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CorydoraSpartan](https://discussions.unity.com/u/CorydoraSpartan)\
**Post date:** [July 29, 2025, 12:07am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/5 "2025-07-29T00:07:55Z")

</div>

Do you have any suggestions on any specific tutorials to learn the unity fundamentals?

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [July 29, 2025, 12:35am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/6 "2025-07-29T00:35:10Z")

</div>

The Unity learn stuff is great… when it works.

Otherwise, Imphenzia has some great stuff (see below).

The NullReferenceError is the most common runtime error you will get in Unity.

It’s like forgetting to tie your shoelace, or your shoelace coming untied. It’s that common.

How to fix a NullReferenceException error

Three steps to success:

- Identify what is null ← any other action taken before this step is WASTED TIME
- Identify why it is null
- Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

> **[How to find a nullref in Unity3D scripting – PLBM Games](http://plbm.com/?p=221)**

Imphenzia: How Did I Learn To Make Games:

[![](https://europe1.discourse-cdn.com/unity/original/4X/8/e/8/8e89f6b74f09dc049f0792c06b2cd8dbcedeb9c5.jpeg "How did I learn to make Games?") ](https://www.youtube.com/watch?v=b3DOnigmLBY)

Two steps to tutorials and / or example code:

1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

---

<div class="post-metadata">

**Author:** ![CorydoraSpartan](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CorydoraSpartan](https://discussions.unity.com/u/CorydoraSpartan)\
**Post date:** [July 29, 2025, 1:01am UTC](https://discussions.unity.com/t/unity-keeps-throwing-up-nullreferenceexception-error-when-my-scripts-say-no-errors/1671825/7 "2025-07-29T01:01:25Z")

</div>

Thanks! This Imphenzia guy seems really knowledgeable.
