EnemyAI NullReferenceException

Hey, i’m trying to make a hack and slash game, based on the tutorials, but, i’m with a problem, that’s my code:

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
public GameObject go;

// void awake(){
//  myTransform = GetComponent<Transform>();
//  }

// Use this for initialization
void Start () {
	myTransform = GetComponent<Transform> ();
	GameObject go = GameObject.FindGameObjectWithTag("Player");
	target = go.transform;
	
}

void Update () {
	Debug.DrawLine (target.position, myTransform.position); 
	
	// Look at target
	myTransform.rotation = Quaternion.Slerp(myTransform.rotation , Quaternion.LookRotation(target.position - myTransform.position) , rotationSpeed * Time.deltaTime);
	
	// Move towards target
	myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime ;
	
	transform.position = myTransform.position;
	transform.rotation = myTransform.rotation;
}

}

it says there’s a null reference exception on the line number 19 : “target = go.transform;” please help me
PS: Sorry my bad English

1 Answer

1

Check there is a game object that has the “Player” tag defined. The error suggests that the line before it did not find a game object with the Player tag.

For the appropriate game object selected, look in the Inspector at the top right. There is a Tag button which allows to select different tags, as well as create new tags. Check the value selected is “Player”.