Error* Scripting problem.. Dont no how to explain I have script and photo!!

I am making my enemy AI off a few online tutorials mixed in one and I can’t find out how to get rid of this error message, I havent figured out what is wrong with the coding, and I hope you can help from the picture and script… It says;

UnassignedReferenceException: The variable target of EnemyAI has not been assigned.
You probably need to assign the target variable of the EnemyAI script in the inspector.
UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransformBindings.gen.cs:20)
EnemyAI.Update () (at Assets/Code/EnemyAI.cs:60)

*Don’t discount this question as un informative to the person to help me, I gave the script and error, i want the error to stop showing up…

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(SphereCollider))]
public class EnemyAI : MonoBehaviour {
    public float preceptionRadius = 5;

    public Transform target;
    public float movementSpeed = 5;
    public float rotationSpeed = 420;

    private Transform myTransform;

	// Use this for initialization
	void Start ()
    {
        SphereCollider sc = GetComponent<SphereCollider>();
        CharacterController cc = GetComponent<CharacterController>();

        if (sc == null)
            Debug.LogError("No Collider on enemy");
        else
        {
            sc.isTrigger = true;
        }

        if(cc == null)
        {
            Debug.LogError("There's no Character Controller");
        }
        else
        {
            sc.center = cc.center;
            sc.radius = preceptionRadius;
        }

        myTransform = transform;

        //GameObject p = GameObject.FindGameObjectWithTag("Player");

        //if (p == null)
        //     Debug.LogError("Could not Find the Player");

        //target = p.transform; 
	}
	
	// Update is called once per frame
	void Update ()
    {
        if (target)
        {
            Vector3 dir = (target.position - myTransform.position).normalized;
            float direction = Vector3.Dot(dir, transform.forward);

            float dist = Vector3.Distance(target.position, myTransform.position);
        }
        
        //Find or Look at Target
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

        //Movement
        myTransform.position += myTransform.forward * movementSpeed * Time.deltaTime;
    }

    public void OnTriggerEnter(Collider other)
    {
        Debug.Log("Entered");
        if (other.CompareTag("Player"))
        {
            target = other.transform;
        }
    }
    public void OnTriggerExit(Collider other)
    {
        Debug.Log("Exit");
        if(other.CompareTag("Player"))
        {
            target = null;
        }
    }
}

The error message very clearly says what the problem is. The variable target doesn’t have a value assigned to it and you’re trying to access it. You can double click on one of the error messages in the console and it will take you to the exact line where the error occurs.

The error message says it’s on line 60… looking at that line from your script:

     myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

It is indeed referencing the target variable. If you look up a few lines you’ll see that the there is a line that says if (target) {, then it it does a few things if that condition is true. That line is there because it’s possible for target to not be set to anything (it’s null) and you don’t want to try to use it if it’s not set.

So most likely line 60 should be inside of that if statement. And probably the next line as well.

You’ve got the following in Update:

if(target)

which is checking for null, that’s correct.
But afterwards you still use the target variable in the Update method without the nullcheck. (in Quaternion.LookRotation)

myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

That’s also what causes the error to happen. Transform.position is a property, properties will be kind of converted into methods, in this case to get_position(), which is the method that occurs in the error code as well.

Try to move it inside the if statement or find an alternative.