I’m making a pretty simple bullethell game, and I would like the enemy to stop shooting when my player dies(it is active when alive, and not active when dead). I don’t know where I fail in the code. It gives me a NullReferenceException when my player dies (gets deactivated).
var enemyLaser : GameObject;
var fireFreq : float;
var projectileSpeed:float = 0.0;
private var lastShot : float;
function Update () {
if (Time.time > lastShot + fireFreq){
Fire();
}
}
function Fire() {
lastShot = Time.time;
var player = GameObject.Find("player");
if (player.active){
var shoot = Instantiate(enemyLaser,transform.position,transform.rotation);
shoot.rigidbody.velocity = (player.transform.position - transform.position).normalized * projectileSpeed;
}
}