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...![alt text][1]
[1]: /storage/temp/54865-error.png
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();
CharacterController cc = GetComponent();
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;
}
}
}
↧