Random.Range works differently as an int than it does as a float, so if the randomRange variable is a float, even if it’s assigned an int value, it’ll change the actual function being called. This is dangerous being Random.Range(int, int) doesn’t include the “max” number itself, just “up to max” (exclusive), while Random.Range(float, float) (and int/float and float/int by implicit conversion) will include the final number as a max value (inclusive). As it is, I have no way of knowing if you’re using “var” (in Javascript or C#) and implying the value types, but that could be messing you up.
Alternatively, randomRange isn’t the value that you think it is, or you’re using the nullable version “int?”. Try Debug.Logging it just before this statement and make sure. Really can’t be more detailed without seeing more of the script- a single line implies that you know exactly what the error is, and if you knew that, you probably wouldn’t be asking for help ^_^.
I’ve done what I could with what I had- if you want additional assistance you’ll really need to post the rest of the script. As Random.Range is built into Unity and running fine for everyone else, it can’t possibly be the problem here.
This is not a good way of creating a random sequence, since you could easily waste thousands of iterations. I actually wrote one the other day for somebody though, let me go see if I can find it.
EDIT: Found it.
using System.Linq;
public List<int> RandomRangeList(int start, int count)
{
List<int> randomList = new List();
List<int> remaining = Enumerable.Range(start, count).ToList();
while(remaining.Count > 0)
{
int randomIndex = Random.Range(0, remaining.Count);
randomList.Add(remaining[randomIndex]);
remaining.RemoveAt(randomIndex);
}
return randomList;
}
As in a freeze? This normally indicates an infinite loop of some sort.
Messing with the iterator inside of a for loop is a good way to do that. Your i-- is adding one more iteration to the loop each time it runs. Never modify i inside a for loop.
If the code ever runs with randomRange set to a value of less then 11, you get an infinite loop.
There are much more efficient ways to generate random orders, @DonLoquacious posted a good one
Thanks for the advice last week. And thank you @DonLoquacious your code worked brilliantly. I went back over the whole code design to make sure the approach was correct and implemented your random range code and it works better than ever.