- Home /
Making a plain number range
I know about Random.Range, but I would like to say "x equals 1 to 10", and using just Range doesn't work. What do I do?
[EDIT] Hmm, ok, maybe it would be better if I explained what I wanted to do. I want my enemies to drop ammunition when they die, but not every time. So I'm using Random.Range to get a number when the enemy dies, which I want the numbers of different types to ammo to be compared to. If they are the same, then spawn that ammo. However, I want some ammo to be more common than others, and the only way I could think of doing that is by making a list, to make the chance of the number getting picked larger.
Answer by aldonaletto · Jun 13, 2011 at 01:10 AM
You can do the following:
if (Random.Range(0,10)<chance){
// drop ammo
}
The chance variable controls how often the ammo will be dropped: chance=0 means never, chance=5 means 50%, chance=10 means always. The chance var can be set to different levels depending on each ammunition type.
@Joshua, in this case, which version of the Random.Range the compiler will choose, integer or float? Or the same type of the chance variable?
@aldonaletto I think it will output an int because the two input variables are also integers. I'm not sure though, I'll test this tomorrow. :)
Answer by Joshua · Jun 13, 2011 at 12:09 AM
var x : int[] = new int[10];
for( var i : int = 0; i<10; i++ ){ x[i] = i+1; }
Read up on arrays here.
I want x to be a list of numbers, so basically "x=1, x=2...".
Hmm, I'm having trouble understanding exactly what to do. I was just trying to make a variable equal a list of numbers.
I'm really not following exactly what you want. Joshua's answer is exactly that, an array of numbers. Whenever you'd want the first number in that array, you'd use x[1]. x[2] will give you the second number. x[3] will give you the third. That seems like a list of numbers to me.
What is the idea? Do you want to vary x from 1 to 10, one at a time?
Your answer
Follow this Question
Related Questions
Number Range 3 Answers
Mathf.Clamp an int value 4 Answers
Does anyone know why this is wrong? 2 Answers
it is not possible to invoke an expression of type int 2 Answers
Checking an objects script to change an int on another script 1 Answer