- Home /
Instantiate for few seconds
I want to create objects for few seconds after colliding it. Could you tell me how to destroy clones after few seconds after colliding and creating them? I'm using C# scripts. Ps. I'm thinking about something like destroying clones 5 seconds after creating them. (sorry if i made mistakes i'm not from USA or England and I don't know English Well... )
using UnityEngine;
using System.Collections;
public class Object_1 : MonoBehaviour {
public Transform prefab;
void OnTriggerEnter(Collider anotherobject)
{
Instantiate(prefab, new Vector3(208.353F, -81.55669F, 179.8901F),new Quaternion(90, 0 ,0, 90));
Instantiate(prefab, new Vector3(208.353F, -81.55669F, 175.8901F),new Quaternion(90, 0 ,0, 90));
Instantiate(prefab, new Vector3(208.353F, -81.55669F, 173.8901F),new Quaternion(90, 0 ,0, 90));
}
}
Answer by whydoidoit · Apr 20, 2013 at 08:59 PM
You can do that by using Destroy with a time:
var newOne = Instantiate(prefab, new Vector3(208.353F, -81.55669F, 179.8901F),new Quaternion(90, 0 ,0, 90)) as Transform;
Destroy(newOne.gameObject, 5);
Or if you prefer:
Destroy((Instantiate(prefab, new Vector3(208.353F, -81.55669F, 179.8901F),new Quaternion(90, 0 ,0, 90)) as Transform).gameObject, 5);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How Do I Instantiate An Object Behind My Character 1 Answer
C# Unity 3D Lock Rotation but allow Rotation of Parent Object 1 Answer
This is a sloppy conversion from javascript to C#, I cannot seem to get it working... 2 Answers
Using a non-monobehaviour class file 1 Answer