I have an array of gameobjects called “Rooms”. I also have an int called “AllRooms”. In my start method, I set “AllRooms” to equal “Rooms.Length”. Now I am looking for a possible way on how to instantiate the rooms and destroy them after 10 seconds of existence. Do I have to use Empty GameObjects to define the next room’s location? I am new to procedural generation, and this is my first attempt on working on an infinite runner game. Also, I was thinking of using the Random.Range term to randomly generate the rooms, as this is one of my main mechanics.
Place this script on all of your rooms:
using UnityEngine;
using System.Collections;
public class KillOnTime : MonoBehaviour {
public float OnTime;
void Awake () {
Destroy(gameObject, OnTime);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Then use the inspector to chose the time before destroying.
If you look at the Instantiate function, you can see that it is possible to give a location (no need for an empty gameobject)
There is another method as well. You said that you are creating the rooms somewhere as well. I assume you are using Instantiate. If not then the above is the best way.
If you are using Instantiate then you can do something like the following at the place you are creating the rooms:
GameObject gameObject = Instantiate (roomBaseObject, postition, rotation);
Destroy (gameObject, 10f);
The 10f in the Destroy call is the time to wait until you want to destroy it.