I have made an array with 3 objects in them. On start two of them are in place and the third is off screen. I have them moving screen left and after they get to a certain point in world space I move them down. What I want to do is randomly pick one of the three object from my array and set it to a set position and start moving it. My sticky point is selecting the object randomly. ANy thoughts?
Yeah. I was thinking that. Though, I’m not quite sure how to link that with the elements in the array.
The other thing I was looking at was the .Sort Does that even work with builtin array lists? Figured that would be easy because then I could just pick the (0)element and it would give me back a different one each time…
var MyArray = new Array("HiggyB", "bedalowe", "unknown");
var MyIndex = Random.Range(0,(MyArray.length - 1));
Debug.Log(MyArray[MyIndex]);
You’ll use Random.Range to get an index that’s within the length of the array than use that as a lookup value to pull the random piece of information.
No, it wouldn’t give you something different every time nor would it be the least bit random. Sort the alphabet (ascending or descending sort), now always pull the first item and you will get a very predictable and very un-random set of results. Sorting has nothing to do with what you’re after so don’t muddy your thoughts with this just yet.
The upper range when using integer values is non-inclusive. (For precisely this reason–using ranges this way is common, so you get easier to read code.)
The technique above works for builtin arrays just fine, and yes they are faster if you can live with them having a fixed size/length and having all one data type within them.
Actually I did figure that out. Sounds like you might have the code outside a function…declaring variables outside functions is fine, but code that “does stuff” should be inside.
Tip: please start using the Code button, or manually wrap code in a code block ([ code ]…[ /code ], without the spaces) when you post code here on the forums. It helps with formatting, I’m going to edit your posts so you can see for yourself.
So, you want to pull two random items from an array now, and ensure that those two items are not the same? Sure, you can use a while loop as a relatively inelegant solution:
var myIndex1 = Random.Range(0, myArray.length);
var myIndex 2 = Random.Range(0, myArray.length);
while (myIndex2 == myIndex1) {
myIndex 2 = Random.Range(0, myArray.length);
}
That while loop should run until something other than the first index value is pulled. If you say “but not working still” then please be specific, what about it is not working? Script error? Pulling duplicate items? Never exits the while? Please note that you might sit in that while loop for quite some time before it randomly pulls some other value, which is why that’s not such an elegant solution.
Another would be to, on the fly, create a temporary array that is nothing but indices, then grab and remove from that array. Example:
myArray = new Array("Tom", "Joe", "Amir", "Jeff");
// Populate the array of indices
myIndices = new Array();
for (var i = 0; i < myArray.length; i++) {
myIndices.Push(i);
}
// Pull one index from the array and remove it
var myIndexPull = Random.Range(0, myIndices.length);
myIndex1 = myIndices[myIndexPull];
myIndices.RemoveAt(myIndexPull);
// Pull another index from the array and remove it as well
myIndexPull = Random.Range(0, myIndices.length);
myIndex2 = myIndices[myIndexPull];
myIndices.RemoveAt(myIndexPull);
Debug.Log(myIndex1 + " and " + myIndex2);
Everytime you’re guaranteed to get two different values as you’re pulling from a list of “available” indices.