Question by
heyhujiao · Dec 20, 2015 at 03:15 PM ·
c#timespawngame object
How to set Game Object to spawn after a certain timing After Level Loads?
Hello guys! I have a script that spawns my kill objects. I want my kill objects to spawn after 5 secs, 20secs, 60secs, 90secs so that I will have a total of 5 kill objects in the game after 90 secs. Any idea how to do that?
The following is my kill object spawning script :
using UnityEngine;
using System.Collections;
public class KillObjectGenerator : MonoBehaviour
{
public float randomTimeDelay;
private float randomTimeDelay2;
private float randomTimeDelay3;
private float randomTimeDelay4;
private float randomTimeDelay5;
public GameObject cloud;
public GameObject lightning;
private float randomX;
private Vector2 newPositionL;
private Vector2 newPositionC;
private Vector2 newPositionL1;
private Vector2 newPositionC1;
private Vector2 newPositionL2;
private Vector2 newPositionC2;
private Vector2 newPositionL3;
private Vector2 newPositionC3;
private Vector2 newPositionL4;
private Vector2 newPositionC4;
void Start()
{
randomTimeDelay = Random.Range(3f, 6f);
randomTimeDelay2 = Random.Range(3f, 6f);
randomTimeDelay3 = Random.Range(3f, 6f);
randomTimeDelay4 = Random.Range(3f, 6f);
randomTimeDelay5 = Random.Range(3f, 6f);
// Start calling the Spawn function repeatedly after a delay.
InvokeRepeating("GenerateFirst", randomTimeDelay, 2.0f);
if (Time.frameCount > (5 * 30))
{
InvokeRepeating("GenerateSecond", randomTimeDelay2, 2.0f);
Debug.Log("2nd one coming to get you!");
if (Time.frameCount > (20 * 30))
{
InvokeRepeating("GenerateThird", randomTimeDelay3, 2.0f);
Debug.Log("3rd one coming to get you!");
if (Time.frameCount > (60 * 30))
{
InvokeRepeating("GenerateForth", randomTimeDelay4, 2.0f);
Debug.Log("4th one coming to get you!");
if (Time.frameCount > (90 * 30))
{
InvokeRepeating("GenerateFifth", randomTimeDelay5, 2.0f);
Debug.Log("5th one coming to get you!");
}
}
}
}
}
void GenerateFirst()
{
float randomX = Random.Range(-5.25f, 6.0f);
newPositionL = new Vector2(randomX, 1.5f);
newPositionC = new Vector2(randomX, 0.23f);
Instantiate(cloud, newPositionC, Quaternion.identity);
Instantiate(lightning, newPositionL, Quaternion.identity);
Debug.Log("Spawned a kill object!");
}
void GenerateSecond()
{
float randomX1 = Random.Range(-5.25f, 6.0f);
newPositionL1 = new Vector2(randomX1, 1.5f);
newPositionC1 = new Vector2(randomX1, 0.23f);
Instantiate(cloud, newPositionC1, Quaternion.identity);
Instantiate(lightning, newPositionL1, Quaternion.identity);
}
void GenerateThird()
{
float randomX2 = Random.Range(-5.25f, 6.0f);
newPositionL2 = new Vector2(randomX2, 1.5f);
newPositionC2 = new Vector2(randomX2, 0.23f);
Instantiate(cloud, newPositionC2, Quaternion.identity);
Instantiate(lightning, newPositionL2, Quaternion.identity);
}
void GenerateForth()
{
float randomX3 = Random.Range(-5.25f, 6.0f);
newPositionL3 = new Vector2(randomX3, 1.5f);
newPositionC3 = new Vector2(randomX3, 0.23f);
Instantiate(cloud, newPositionC3, Quaternion.identity);
Instantiate(lightning, newPositionL3, Quaternion.identity);
}
void GenerateFifth()
{
float randomX4 = Random.Range(-5.25f, 6.0f);
newPositionL4 = new Vector2(randomX4, 1.5f);
newPositionC4 = new Vector2(randomX4, 0.23f);
Instantiate(cloud, newPositionC4, Quaternion.identity);
Instantiate(lightning, newPositionL4, Quaternion.identity);
}
}
Comment