Quantcast
Channel: Answers for "How to randomly iterate through a list of objects without repeating?"
Viewing all articles
Browse latest Browse all 7

Answer by Addyarb

$
0
0
My solution would be something a little more simple/approachable than most here. This won't be as "Random" as the others mind you, but the player won't know it anyway either way, right? int CurrentID; public GameObject[] myArray; GameObject mySelectedGameObject; void Start(){ InvokeRepeating ("SelectRandom", 0, 1); } void SelectRandom(){ int lastID = CurrentID; int randomInt = Random.Range (0, myArray.Length); if (randomInt != lastID) { mySelectedGameObject = myArray [randomInt]; CurrentID = randomInt; } else { if (lastID != myArray.Length-1) { mySelectedGameObject = myArray [++lastID]; CurrentID = lastID + randomInt; } else{ mySelectedGameObject = myArray [--lastID]; CurrentID = lastID + randomInt; } } }

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>