What is the meaning of this error?

NullReferenceException: Object reference not set to an instance of an object
Shooter.IsAttackerInLane ()
Shooter.Update ()

code:
private bool IsAttackerInLane()
{
if(myLaneSpawner.transform.childCount <= 0)

      {
        return false;

      }
    else
      {
        return true;
         
      }
}

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

because of this error animation is not running when attacker is in lane

NullReferenceException means you are trying to use something (myLaneSpawner in this case I am guessing) before it has been set/assigned to anything.

So look for where you are setting myLaneSpawner - is it a serialized field? Did you set it to something in the inspector? Or are you assigning to it in code? Has that code run before the Update() and IsAttackerInLane() methods were called?

1 Like

Thanks for your responce sir. myLaneSpawner is not serialized field and it is assign in code.and IsAttackerInLane() method is not running
here’s my code:
private void SetLaneSpawner()
{
AttackerSpawner spawners = FindObjectsOfType();

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

        if(IsCloseEnough)
        {
            myLaneSpawner = spawner;
        }

    }
}
NullReferenceException: Object reference not set to an instance of an object
Shooter.IsAttackerInLane ()
Shooter.Update ()

The above specifically means that the Update() method on Shooter did run, and it called IsAttackerInLane(), so I disagree when you say it is not running. The stack trace shows the (reverse) order of the calls that are made and running.

Your new snippet does not confirm any of the following:

  1. SetLaneSpawner() was called at any time prior to Shooter.Update()
  2. Whether any spawners were found by the FindObjectsOfType call.
  3. That any of them were ‘IsCloseEnough’ to be set.

Specifically that last point, you’re trying to say “if the absolute Y distance between these two objects is less than or equal to the smallest float value possible, only then set myLaneSpawner = spawner”.

Chances are that is never becoming true, and therefore myLaneSpawner is never being set. You likely needed to add some additional tolerance value in there (although that also isn’t a guarantee any would be true), or to have a temporary object that stores “the closest” spawner found so far, and to keep replacing it any time in your loop that you find another one that is even closer than that.

Please try putting some additional Debug.Log() statements throughout your code in that area and see if your logic is working as you thought it would, and also whether it is happening before Shooter.Update() is being called (perhaps it’s not).

1 Like

Thanks for responce sir.I just fixed my problem. It turns out my parent spawner was not set to zero position Once I fixed that, my code is working.

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

Privacy & Terms