var prefab : GameObject;
var timer : float = 0.0;
var respawnrate : float = 120;
function Start () {
timer += Time.deltaTime;
if(timer == respawnrate){
var position = Vector3(UnityEngine.Random.Range(-10, 10), 0, Random.Range(-10, 10));
Instantiate(prefab, position, Quaternion.identity);
}
}
/* The command prompt says Range is not a member of random....HELP*/
3 Answers
3try using UnityEngine.Random.Range()
exactly, Random is a namespace elsewhere also so you need to specify the UnityEngine namespace.
– anon95292375The command prompt says Range is not a member of random....HELP
I find the only possible explanation for this error that your script is called Random.js, in which case Random.Range would try to access the function 'Range' in your class 'Random', which doesn't exist.
To solve this issue you must type out the full name of the UnityEngine.Random class, or rename your class to something else. You had correctly qualified the first call to Random.Range, but not the other. The below code should work fine.
var prefab : GameObject;
var timer : float = 0.0;
var respawnrate : float = 120;
function Start () {
timer += Time.deltaTime;
if(timer == respawnrate){
var position = Vector3(UnityEngine.Random.Range(-10, 10), 0, UnityEngine.Random.Range(-10, 10));
Instantiate(prefab, position, Quaternion.identity);
}
}
Hello Kyle.
The problem you're having is that you don't need to use the UnityEngine prefix.
Random.Range should do the work.
Also to note is that you're saying Random.Range(-10, 10). -10 and 10 are both of type int, and the script reference for Random.Range (min : int, max : int) says that min will be inclusive and max exclusive in the range. Thus, you will have every integer between -10 and 10 only. You can also use variables of type float instead of int to include every single number (including decimals) in the range of -10 and 10 (when using float, both numbers are inclusive).
Example:
function Start () {
timer += Time.deltaTime;
if(timer == respawnRate){
var random1 : int = Random.Range(-10, 11); // Every integer between -10 and 11 (excluding 11).
var random2 : float = Random.Range(-10, 10); // Every real number between -10 and 10 (including 10).
var position = (random1, 0, random2);
Instantiate(prefab, position, Quaternion.identity);
}
}
The problem isn't using the prefix, I ran OPs above code just fine. In the case of a namespace clash, compiler would said: BCE0004: Ambiguous reference 'Random':
Also your comment about random2 is wrong...what matters is the type of variables you're using in Random.Range, not the type of the variable you're assigning to. You're using integers in Random.Range, so it would return integers from -10 through 9 regardless of whether random2 is a float or not.
– Eric5h5Totally right about random2, sorry. And I have no clue then about the Ambiguous reference..... Sorry... I was trying to help.... :(
– anon21219440The funny thing is that the example in the docs is also wrong ^^. http://unity3d.com/support/documentation/ScriptReference/Random.Range.html
– Bunny83@joeyrubio: Corrections are not punishments :) It's good to help. Downvotes might feel bad, but on the other hand you learned something new, and that is worth more than some silly rep points. Rep points you can't use. Wisdom, you can. Don't feel shunned away from answering questions :)
– Statement
Have you tried it without the UnityEngine prefix?
– anon43051525It wouldn't matter. If there would been a namespace clash the error would have been
– StatementBCE0004: Ambiguous reference 'Random':