- Home /
Avoid getting same object as previous one with Random.Range?
Hey guys, been working with Random.Range today and I am trying to avoid getting the same result as the previous one when i randomly select an object from an array, here is the code:
var myArray : GameObject[]; //array
function Start (){
myArray = gameObject.FindGameObjectsWithTag ("point");//fill array with objects
}
function Update(){
if (Input.GetButtonDown("Fire1")){
RandomPick();
}
}
function RandomPick(){
var myIndex = Random.Range(0, myArray.length);//randomly pick an object
Debug.Log(myArray[myIndex]);//output selected object
}
What would be the simplest way of preventing myIndex from selecting the same object as the previous one?
Same as any previous item, meaning never a repeat? Or just not the immediate previous -- can't pick the same thing twice in a row, but could pick it every other?
I meant not the immediate previous.I already solved the problem by creating another array and every time one object was selected, it was placed on the second array for 5 seconds, preventing the Random.Range from immediately selecting it again.
Answer by fendorio · Apr 17, 2014 at 04:15 AM
Remove it from the array? Other than that what you want isn't really random..
My JS is rusty to be fair haha!
function RandomPick(){
var myIndex = Random.Range(0, myArray.length);//randomly pick an object
Debug.Log(myArray[myIndex]);//output selected object
myArray.splice(myIndex,1); //removes the index:myIndex
}
So yeah splice does the work here:
splice(index, numberOfTimes);
index = The index of the element you want to remove numberOfTimes = Number of items to be removed - 0 results in 0 items being removed.
If you want to store the object you just removed instead of 'just' removing it, simply add:
var x = myArray.splice(myIndex,1);
I have no idea why my 6 year old question has been getting attention today. This code snippet was written in Unityscript, a previously supported program$$anonymous$$g language made for Unity. Unity dropped support for it and for Boo a long time ago. Try using something like foos.RemoveAt(index);
ins$$anonymous$$d
Answer by endasil_unity · Apr 20, 2020 at 02:10 PM
I would solve it like this if it is just the last value you do not want repeated.
var myArray : GameObject[]; //array
// Keep track of last selected index
var previousPick = -1;
function Start () {
myArray = gameObject.FindGameObjectsWithTag ("point");
}
function RandomPick(){
var myIndex = previousPick;
// loop until the random value does not match last one
while(myIndex == previousPick)
{
myIndex = Random.Range(0, myArray.length);
}
// update last one.
previousIndex = myIndex;
}
Haha better late than never they say! :P I only checked posts on the first two pages, i wonder how something 6 years old arrive there? I assumed they would be ordered by time. Do they randomly pop up questions without anything marked as correct answer? :)
No, someone posted a quite irrelevant comment stating that an array doesn't have a slice method -.- That comment was actually rejected by moderators but the bump is already there. The only way I know to revert such a bump is by reporting the comment and deleting it through the reported posts page. This will remove the bump. $$anonymous$$oving a post to moderation and rejecting it does not undo the bump since technically the post is still there, just not visible.
Note that your answer has two issues:
First UnityScript is a thing of the past. $$anonymous$$ost people using Unity use a more or less recent version which does no longer support UnityScript. So it's generally better to provide code samples in C# now.
Using a while loop like that has a potential risk for causing a hang (soft crash) of your application. This happens when the array just contains a single element (or even zero). It's in general dangerous to use any code like this without any safety measures. It might work now but since it depends on external data (specifically gameobjects with the "point" tag) this is out of the control of this code.
Your answer
Follow this Question
Related Questions
Instantiate a random prefab at an objects location 3 Answers
Random Range with limits 2 Answers
How to assign Transform to prefab ? 2 Answers
Scene object to prefab 1 Answer