Variable not assigned-but it is?

Ver: 2019.3.13f1
Code:

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

public class EnemyAI : MonoBehaviour
{
    public Transform enemyCenter;
    public GameObject BulletPrefab;
    Transform target;
     void Start()
{
     if(enemyCenter == null)
         Debug.LogError("SomeVariable has not been assigned.", this)    ;
     // Notice, that we pass 'this' as a context object so that Unity will highlight this object when clicked.
}
   
    // Update is called once per frame
    void FixedUpdate()
    {
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        Vector3 dir = enemyCenter.position - target.position;
        float angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle -270, Vector3.forward);
    }
}

I assigned EVERY variable. And there’s only one prefab in the scene.

You posted code…

But what happens? What do you expect to happen? Is there an error? What does that error say? Where does that error point?

That may be true above.

But you should be more concerned with where the actual error is located.

The compiler is always correct.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

Fixed. So it turns out I pulled a stupid and assigned the script to the gun, and also the enemy center. So, that’s fixed!

Oh, this was just a bog standard nullref?!!! Why didn’t you say so!

There’s only ONE three-step process to fix those. Every other approach is simply wasting time.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that