Time delay

Hi i’m trying to implement traps which some enemies can destroy after colliding into them after 3 secs. Well here is my code

public class EnemyMoveScript : MonoBehaviour {
	
	public enum EnemyType
	{
		wee=0,
		big,
		flying,
	}
	
	public EnemyType enemyType;
	
	public float speed=0.2f;
	public Vector3 direction=Vector3.up;
	public float startTime=0.0f;
	public float destroyTrapTime=3.0f;

	// Use this for initialization
	void Start () {
	
	}

	// Update is called once per frame
	void Update () {
	transform.Translate(direction*speed*Time.deltaTime);
	}
	
	public void OnCollisionEnter(Collision other)
	{
		if(other.gameObject.tag=="Trap")
		{
			if(enemyType == EnemyType.wee)
			{
                                startTime += Time.deltaTime;
				if (startTime>destroyTrapTime)
				{
					other.gameObject.active=false;
				}
			}
		}
	}
}

Everything was working fine until i added

  startTime += Time.deltaTime;
				if (startTime>destroyTrapTime)

Please help!

i usually use (just convert it to C#):

var delay : float = 3.0;
private var timer : float = 0;

function Start(){
   timer = Time.time + delay; // launch timer
}

function Update(){
   if (timer<Time.time){
      //Do something
   }
}

Ups! You cant use OnCollisionEnter but you must place it in Update or use yield. look in documentation about yield WaitForSeconds(). in js you would write:

yield WaitForSeconds(5);
other.gameObject.active=false;

But i dont know maybe in C# it looks different.

Thanks for the reply still having some troubles with this though. i’ve updated the code to create a boolean enemyCollison =true if the collider is activated and then tried to put the resulting code in the update function but keep getting an error “The name `enemyCollison’ does not exist in the current context”.

public class EnemyMoveScript : MonoBehaviour {
	
	public enum EnemyType
	{
		wee=0,
		big,
		flying,
	}
	
	public EnemyType enemyType;
	
	public float speed=0.2f;
	public Vector3 direction=Vector3.up;
	public float startTime=0.0f;
	public float destroyTrapTime=3.0f;
	private GameObject other;
	private bool enemyCollision = false;
    

	// Use this for initialization
	void Start () {
	
	}

	// Update is called once per frame
	void Update () {
	transform.Translate(direction*speed*Time.deltaTime);
		if(enemyCollision==true)
		{
			startTime += Time.deltaTime;
			    if (startTime>destroyTrapTime)
				{
					other.gameObject.active=false;
				}
			}
	}
	
	public void OnCollisionEnter(Collision other)
	{
		if(other.gameObject.tag=="Trap")
		{
			if(enemyType == EnemyType.wee)
			{
				enemyCollison = true;
				speed=0.0f;
                direction = Vector3.zero;
				
               /*startTime += Time.deltaTime;
			    if (startTime>destroyTrapTime)
				{
					other.gameObject.active=false;
				}*/
			}
		}
	}
}

A bit late on the reply here, but…

You declared:
private bool enemyCollision = false;

And the later assign:
enemy__Collison__ = true;

It should be enemyCollision = true;