Need help with my enemy spawn script.

Ok so I made the changes to the script as follows:

#pragma strict

var emily : GameObject;
var emilyspottedSound : AudioClip;
var target : Transform;
var safeDistance = 10; //How close the player can get

function Start () {
renderer.enabled = false;
collider.enabled = false;

}



   function Update(){
     
    //check how close the player is
    var distanceToPlayer = Vector3.Distance(this.transform.position, target.transform.position);
     
    // this checks to see if the player is too close then runs the spawn function
    if(distanceToPlayer <= safeDistance){
    spawn();
    }
	
     if (renderer.enabled == true){
			destroy();
		}
    
     
    

 }
 
	
	//this is your spawn function
	function spawn (){
    //look at the target
	renderer.enabled = true;
	collider.enabled = true; 
	transform.LookAt(target);
	audio.Play();
	}
	
	function destroy(){
	yield WaitForSeconds (0.5);
	Destroy(gameObject);
		
	}

It unfortunately still doesn’t work. She just stands there lol.

1 Answer

1

I would guess that it doesn’t work because you disable the emily gameObject and hence it can’t check if it’s near the player etc.

Just use renderer.enabled = false/true (and colider.enabled = false/true) instead, this way emily will still be there and all her scripts will work in the background, but the player won’t see her if she isn’t supposed to be there.

Thank you I will close this question as i now understand alot more about my coding. I now feel silly for the question in the first place lol