This is my first ever script I made for AI in unity.What im trying to accomplish is the AI (a cube) to shoot when he is at a distance of the player,but as soon as the AI enters the shooting range it throws : “Object reference not set to an instance of an object”. Here is my code:
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 0;
var MinDist = 0;
var enemybullet :GameObject;
var barrelEnd :Transform;
function Shoot(){
var enemybulletInstance : Rigidbody;
enemybullettInstance = Instantiate(enemybullet, barrelEnd.position, barrelEnd.rotation);
enemybulletInstance.AddForce(barrelEnd.forward * 5000);
}
function Start ()
{
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
Shoot();
}
}
}
I do not have any idea what can cause this since I did everything correctly as far as I know.Any help is appreciated.