NullReferenceException: Object reference not set to an instance of an object

Updated Fri Jan 18 2019 21:12

Every time I place a defender, it stops working and gets this error.

My code is below this, what should I do?

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


public class Shooter : MonoBehaviour
{

    [SerializeField] GameObject projectile, gun;
    AttackerSpawner myLaneSpawner;

    private void Start()
    {
        SetLaneSpawner();
    }

    private void Update()
    {
        if (IsAttackerInLane())
        {
            Debug.Log("shoot pew pew");
            // TODO change animation state to shooting
        }
        else
        {
            Debug.Log("sit and wait");
            // TODO change animation state to idle
        }
    }

    private void SetLaneSpawner()
    {
        AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();

        foreach (AttackerSpawner spawner in spawners)
        {
            bool IsCloseEnough =
                (Mathf.Abs(spawner.transform.position.y - transform.position.y)
                <= Mathf.Epsilon);
            if (IsCloseEnough)
            {
                myLaneSpawner = spawner;
            }
        }
    }

    private bool IsAttackerInLane()
    {
        if (myLaneSpawner.transform.childCount <= 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public void Fire()
    {
        Instantiate(projectile, gun.transform.position, transform.rotation);
    }

}

I have the same Problem now… do you have the solution?

Perhaps share the details of the specific error message, including the line number it refers to, and paste a full copy of the script it relates to.

NullReferenceException: Object reference not set to an instance of an object
Shooter.IsAttackerInLane () (at Assets/Scripts/Shooter.cs:46)
Shooter.Update () (at Assets/Scripts/Shooter.cs:19)

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

public class Shooter : MonoBehaviour
{
    [SerializeField] GameObject projectile, gun;
    AttackerSpawner myLaneSpawner;
    Animator animator;

    private void Start()
    {
        SetLaneSpawner();
        animator = GetComponent<Animator>();
    }

    private void Update()
    {
        if(IsAttackerInLane()) //LINE19
        {
            animator.SetBool("IsAttacking", true);
        }
        else
        {
            animator.SetBool("IsAttacking", false);
        }
    }

    private void SetLaneSpawner()
    {
        AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();

        foreach (AttackerSpawner spawner in spawners)
        {
            bool isCloseEnough = (Mathf.Abs(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon);

            if(isCloseEnough)
            {
                myLaneSpawner = spawner;
            }
        }
    }

    private bool IsAttackerInLane()
    {
        if(myLaneSpawner.transform.childCount >= 0) //LINE46
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public void Fire(float damage)
    {
        Instantiate(projectile, gun.transform.position, transform.rotation);
    }
}

Hi,

Based on the above your variable myLaneSpawner is equal to null, you are receiving the NullReferenceException error because you are then trying to access one of its members, in this case the transform.

If you look at this object and follow it back to where you initialise it that will lead you to the SetLaneSpawner method, and, with that the if(isCloseEnough) statement is the only place where the myLaneSpawner gets initialised.

Things you can check in that method, firstly, is this statement returning anything;

AttackerSpawner[] spawners = FindObjectsOfType<AttackerSpawner>();

It should, but is it, if you don’t have an AttackerSpawner.cs script component on at least one GameObject in the scene then it won’t return anything.

Easy way to test this, add a Debug.Log statement after it;

Debug.Log("AttackSpawners in Scene : " + spawners.Length);

Does this output a value to the console when your game runs?

Assuming it does, add an else statement after your if statement, like this;

else
{
    Debug.Log("isCloseEnough wasn't true");
}

Does this output to the console? If so, then you have pin pointed the issue, either with this line of code;

bool isCloseEnough = (Mathf.Abs(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon);

or, your positioning of your GameObjects.

Hope this helps :slight_smile:

1 Like

Okay, thanks in advance!
-Well the output of spawners.lenght was correct.
-isCloseEnough is indeed not true.

I found it! Thank you!
I am sure you spotted it and I thank you again for the debug-practice!

Now let’s write some errors! Wuhu!
Have a nice evening Rob!

1 Like

You’re very welcome Dominik :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms