- Home /
how can i random a spawning but not same gameobject
I need help. i dunno how to explain but its like this if animal 'a' is respawn, the next respawn is 'b' or 'c' , if animal 'b' is respawn, the next respawn is 'a' or 'c' , if animal 'c' is respawn, the next respawn is 'a' or 'b'
IEnumerator SpawnAnimal(float time){
yield return new WaitForSeconds (time);
Vector3 temp = transform.position;
temp.x = Random.Range (x1, x2);
Instantiate (animals [Random.Range (0, animals.Length)], temp, Quaternion.identity);
StartCoroutine (SpawnAnimal (Random.Range (4f, 5f)));
}
Good Day and thanks before
Answer by WinterboltGames · Jul 22, 2018 at 06:54 PM
Try this code...
public class Spawner : MonoBehaviour
{
[SerializeField] private float spawnRate;
[SerializeField] private GameObject[] prefabs;
private int lastIndex = -1;
private float counter;
private void Update()
{
if (counter >= spawnRate)
{
Spawn();
counter = 0f;
}
counter += Time.deltaTime;
}
private void Spawn()
{
if ((lastIndex + 1) < prefabs.Length)
{
lastIndex += 1;
}
else
{
lastIndex = 0;
}
Instantiate(prefabs[lastIndex], position, rotation);
}
}
@$$anonymous$$illerOfS$$anonymous$$m7 whoaa.. thanks for helping. its work for me.
Your answer
Follow this Question
Related Questions
Simple random spawning system for 10 items without duplicates 2 Answers
How to get Buildbox Set Up in Unity 2D 0 Answers
How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer
Random.range multiple instantiations without repetition 1 Answer
How to spawn randomly positioned objects without overlapping 2 Answers