i am working on a game where i need balls to generate by themselves at random intervals. from my research i found that i should use invoke to do this. if someone could help me with the process of getting this to work, it would be much appreciated.
I’m going to assume you know how to instantiate an object from a Prefab:
You will need to assign a minWait and maxWait in the Inspector. This code should spawn objects constantly with a random delay between min and maxWait
using UnityEngine;
using System.Collections;
public class CameraRotateFollow : MonoBehaviour {
public float minWait;
public float maxWait;
private bool isSpawning;
void Awake()
{
isSpawning = false;
}
void Update()
{
if (!isSpawning)
{
float timer = Random.Range(minWait, maxWait);
Invoke("SpawnObject", timer);
isSpawning = true;
}
}
void SpawnObject()
{
// Code to spanw your Prefab here
isSpawning = false;
}
}
6 Likes
Thank you Takatok,
I was going insane with that one.