- Home /
Procedural Generation...
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.
Please consider the fact that all my work is in C#.
Thanks in Advance!
Answer by dorpeleg · Mar 31, 2013 at 09:22 PM
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)
Thank you very much! Now I understand it all - it is really a simple concept...
Hmmmm... I always use WaitForSeconds in my scripts...
The problem with WaitForSeconds is that it stops the script completely for the amount of seconds.
So it might not always be the best way to wait for something.
Answer by Hotshot10101 · Apr 02, 2013 at 01:05 AM
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.
Your answer
Follow this Question
Related Questions
Procedurally Generated Room System... 1 Answer
how to procedurally generate submarines with random events on the sub? 0 Answers
Procedural Random Walk Dungeon Generator? 2 Answers
Random Spawning 1 Answer
Randomly Generated Levels 3 Answers