Enemy simple AI not responding at all NullReferenceException: Object reference not set to an instance of an object

The following is my code in its exact form. It is supposed to get the player’s position and the enemy’s position and move the enemy toward the player in a constant fashion. Unfortunately, when I press “play”, the game runs but I get a complaint from my debug window that says “NullReferenceException: Object reference not set to an instance of an object” with no action on the part of the enemy, but if I apply the code to the player, he moves on his own. Any Google searches that I do result in a wide range of possible issues, most of which are not specific to Unity, leading me to believe it is an issue with my Javascript syntax and not with my Unity API, but I am not really sure. Bear in mind that I am new to coding in general, and may have actually mixed C# and JS syntax on accident. :slight_smile:

#pragma strict


function Update () {
 var moveDirection : Vector3 = Vector3.zero;

var controller : CharacterController = GetComponent(CharacterController);

//Get Player's position and assign X and Y to a variable.

var plr = GameObject.Find("Player");
	var playerPos:Vector3 = plr.transform.position;
	var PlX = playerPos.x;
	var PlZ = playerPos.z;
	
//Do the same for enemy.
	
var enm = GameObject.Find("Enemy");
	var enmPos : Vector3 = enm.transform.position;
	var enmX = enmPos.x;
	var enmZ = enmPos.z;
	
	//This section is as of yet incomplete, but should result in *some* action.
	
		if (enmX < PlX){
		
		moveDirection.x = 1;
		
		}
			if (enmX>PlX){
			
			moveDirection.x = -1;
			moveDirection.z = 0;
			
			}
			
				//Just to make sure the above section is working.
				Debug.Log(enmX);

	//Next two lines are just for testing purposes. Uncommenting them still threw the error and resulted in no motion from "Enemy".
	
//moveDirection.x = 1;
//moveDirection.z = 1;

//Tells the enemy character to move.
	
controller.Move( moveDirection * Time.deltaTime);
			
}

Providing the error text is very helpful in figure out these problems. I see three potential ways you could get a null reference. If no object in the scene has the name “Player”, if no object in the scene has the name “Enemy” or if there is no character controller on the object this script is attached to.

I’m betting it is the last issue. Based on your comment you are trying to move the enemy, line 7 should be moved to between line 18 and 19 and be rewritten to:

var controller : CharacterController = enm.GetComponent(CharacterController);

While not directly to your issue, GameObject.Find() is a relatively expensive operation. consider changing your code to use tags instead of names, or better yet, initializing your variables in start:

private var enm : GameObject;
private var plr : GameObject;
private var controller : CharacterController;

function Start() {
    plr = GameObject.Find("Player");
    enm = GameObject.Find("Enemy");
    controller : CharacterController = GetComponent(CharacterController);
}