Timed Selfdestruct OnTriggerEnter

Hey,
i’m farely new to making scripts in unity, so there is probably a realy simple solution to this problem.
Iplaned on making a small project, a game a litle bit like TempleRun but completely in 3D and and first person.
So I want the next patform for you to apear as soon as you hit a collider of the previous one(I used Instantiate for that) and at the same time delete the old platform after let’s say 2 seconds. Every time i want to add the timer Visual Studio gives me an error.
Please can somebody help me???

public Object Platform;  
public float timer = 2.0f;  

}
 void OnTriggerEnter(Collider other)
{ 
    Instantiate(Platform, new Vector3(0, 0, 1), transform.rotation);
    yield return new WaitForSeconds(timer);
    Destroy(this.gameObject);
}

The reason for the error is that you can only use yield return new WaitForSeconds(timer) inside a coroutine, i.e. a method with the IEnumerator return type. You are using the yield statement inside OnTriggerEnter which has a void return type, and hence the error.

So, try:

void OnTriggerEnter(Collider other)
 { 
     Instantiate(Platform, new Vector3(0, 0, 1), transform.rotation);
	 
     StartCoroutine(TimedSelfDestruct());
 }

IEnumerator TimedSelfDestruct()
{
     yield return new WaitForSeconds(timer);
     Destroy(this.gameObject);
}

See:

Thank you so much pako. I did’t realy got all the stuff from all those sources