- Home /
Not repeating random
I have a code that randomly spawn enemies. How can i make so that the enemies spawed dont repeat?
The code goes like this
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true) {
for (int i = 0; i < hazardCount; i++) {
platform = Random.Range (1, 4);
switch (platform) {
case 1:
Instantiate (prefab1, new Vector3 (0, 6, 0), Quaternion.identity);
break;
case 2:
Instantiate (prefab2, new Vector3 (0, 6, 0), Quaternion.identity);
break;
case 3:
Instantiate (prefab3, new Vector3 (0, 6, 0), Quaternion.identity);
break;
}
if (!dead) {
yield return new WaitForSeconds (Random.Range (spawnWaitStart, spawnWaitEnd));
}
if (dead) {
yield return new WaitForSeconds (100);
}
}
}
}
Thank you!
your answer doesn't make sense like this. You mean that you want to spawn randomly only the three different prefabs? Cause it's the only way for not repeating them.
I mean: If i have objects 1 2 3 i don't want them to apear 1 1 1 2 3 3 3 3 but ins$$anonymous$$d 1 3 2 1 3 1 2 3. Not the same one after the other
tks
Answer by Mmmpies · Feb 06, 2015 at 02:42 PM
Well if Answers can stay up long enough I'll post this (4th attempt!).
This is just example code but it stores a List of three numbers and removes the one picked. When it gets to 0 it rebuilds the list. Only an example so if you can't work out what to do with the list let me know.
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // IMPORTANT ADD THIS TO USE LISTS
public class ListTest : MonoBehaviour {
private List<int> EnemyCountList = new List<int>();
private int myInt;
// Use this for initialization
void Start () {
LoadEnemyList();
}
// Update is called once per frame
void Update () {
myInt = Random.Range (0,EnemyCountList.Count);
Debug.Log ("How many in the list = " + EnemyCountList.Count + " Selected Int = " + EnemyCountList[myInt]);
// instantiate your enemy your enemy number is EnemyCountList[myInt];
EnemyCountList.Remove(EnemyCountList[myInt]);
if(EnemyCountList.Count == 0)
LoadEnemyList();
}
void LoadEnemyList()
{
EnemyCountList.Add(1);
EnemyCountList.Add(2);
EnemyCountList.Add(3);
}
}
EDIT
OK so you just don't want the same one as last time but any other order is OK?
private int lastLoaded = 0;
// after platform = Random.Range(1,4)'
if(platform == lastLoaded)
int tempRand = Random.Range(0, 2);
switch (platform) {
case 1:
if(tempRand = 0)
platform = 2;
else
platform = 3;
break;
case 2:
if(tempRand = 0)
platform = 1;
else
platform = 3;
break;
case 3:
if(tempRand = 0)
platform = 1;
else
platform = 2;
break;
}
// after you instantiate the object
lastLoaded = platform;
add that and it should stop the previous enemy from instantiating, not tested that code though so watch for errors.
That's a bit more of a constraint than asked for (it won't allow the example acceptable sequence given in the comment - although to be fair I just noticed that the comment came after your answer!).
All he needs to do is keep track of which one was generated last time, and either flip a coin to choose between the other two or (more easily extended to larger sets) throw away and randomise again if the same one is picked twice in a row.
Is anyone else having issues with Answers, I posted my flip a coin method plenty before your Comment on flipping coins @Bonfire Boy, keeps kicking me out as well.
Answer by Bonfire-Boy · Feb 06, 2015 at 04:04 PM
Just keep track of what the last one was, and re-randomise if you pick it again next time.
int lastPlatform=-1;
while (true)
{
for (int i = 0; i < hazardCount; i++) {
platform = Random.Range (1, 4);
while (platform == lastPlatform)
{
platform = Random.Range(1,4);
}
lastPlatform = platform;
switch (platform) {
// rest of it as in the question.
Your answer
