0

I keep on getting the error

UnassignedReferenceException: The variable player of EnemyAI has not been assigned. You probably need to assign the player variable of the EnemyAI script in the inspector.

I am kind of new to all this scripting stuff, and I tried to check other forums, and they didn't help at all, but made me more confused. Here's the script:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

    public Transform player;
    public float playerDistance;
    public float rotationDamping;
    public float moveSpeed;

    //Use this for initialization
    void Start () {

    }

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

        playerDistance = Vector3.Distance (player.position, transform.position);

        if (playerDistance < 15f) {
            lookAtPlayer ();
        }
        if (playerDistance < 12f) {
            chase ();
        }


    }

    void lookAtPlayer()
    {
        Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
        transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);

    }

    void chase() 
    {
        transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}

}

Any Ideas?

panda.o24
  • 1,888
  • 1
  • 13
  • 14

2 Answers2

0

Your variable player, which is of type Transform is used at

Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);

before assigning any values to it. I believe Transform is another class in your project and player is an object of that kind. You probably want to instantiate the object before trying to use it.

Something like

player = new Transform(...);
Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
  • Unity components should not be created using constructors, and a `Transform` component is always added automatically to game object upon creation and can not be added. – Max Yankov May 07 '15 at 07:46
0

In C#, references (such as your EnemyAI reference) can be set to Null, meaning that they are pointing nowhere. This leads to a lot of NullReferenceException errors, when your code tries to run some methods on an null reference.

I suspect that Unity created a special mechanism to prevent this errors from happening to you. Public fields of Unity's MonoBehaviours are used to set up MonoBehaviour's instances in the editor, before the game is even run. MonoBehaviours are typically not created from scratch, in 99% they are deserialised from instances that were edited and saved in the editor.

So, with this error, Unity warns you that you didn't assign any value to this particular instance of MonoBehaviour. To fix this, open editor, select the object that this error refers to (usually, you can just click the error in console and the object will get selected), set the reference to something, and the error will be fixed.

However, judging from context of your script, you may choose a different path. Most likely you have only one player in the scene, and at the same time you may have hundreds of enemies — you don't want to assign their references to the player a hundred of times! And if you'll want to use prefabs, you won't even be able to. So, instead of making this a public field, make it private, and assign it to this field at the initialisation, using Start method. FindGameObjectWithTag("Player") is one possible way to do that; however, in my experience, relying on tags (and strings in general) is sub-optimal in a strong, statically typed language such as C#, and I would use a Singleton or Service Locator patterns to find the player instead.

Community
  • 1
  • 1
Max Yankov
  • 12,551
  • 12
  • 67
  • 135