- Home /
Script not working (Java script)
So, I need some help with my script. I am working on an item spawn script which randomly generates a number between 0 and 20. However, whenever I try and play it gives me this error :
MissingMethodException: UnityEngine.Mathf.Random
Boo.Lang.Runtime.DynamicDispatching.MethodDispatcherFactory.ProduceExtensionDispatcher ()
Code :
var Crisps : GameObject;
var Water : GameObject;
var Beets : GameObject;
var Cola : GameObject;
var Nuka : GameObject;
var Anime : GameObject;
var Pepsi : GameObject;
function Start(){
var random = Mathf.Floor(Mathf.Random() *20);
if(random.Equals(0)){
Instantiate(Crisps, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
if(random.Equals(3)){
Instantiate(Water, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
if(random.Equals(5)){
Instantiate(Beets, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
if(random.Equals(6)){
Instantiate(Cola, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
if(random.Equals(9)){
Instantiate(Nuka, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
if(random.Equals(16)){
Instantiate(Anime, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
if(random.Equals(19)){
Instantiate(Pepsi, this.gameObject.transform.position, this.gameObject.transform.rotation);
}
}
So, I have also tried another way I have done it using different Math
Code
Now: var random = Mathf.Floor(Mathf.Random() *20);
(Gives error as above, doesn't spawn)
Before: var random = Random.RandomRange(0, 20);
(No error, only spawns 1 item out of the 7 and only in one place)
well, your code from before was correct, the only thing you need to understand is that since you are calling it on "Start function" is only called "once" and it won't happen again, if you want to happen more times use "Update Function"
As @FrankStars$$anonymous$$o said, you should be using Random.Range since there is not $$anonymous$$athf.Random function (hence the $$anonymous$$issing $$anonymous$$ethod Exception). If you want this to run more than once on Start you need to have some type of iteration like a for loop.
The question isn't about running it more than once; that's a separate issue.
Answer by Eric5h5 · Oct 06, 2013 at 03:47 PM
There is no Mathf.Random. There also isn't any Random.RandomRange (or rather it's obsolete). As you can see in the docs (open the scripting docs and type "random" in the search box), you should use Random.Range. Since you're only checking for 7 possibilities out of 20, naturally that can't work. Presumably you want to check for ranges, not individual values? You'd need to use >=, such as:
if (random >= 19) ...
else if (random >= 16) ...
else if (random >= 9) ...
...
However it would be a better idea to use arrays instead of hard-coding all these if/else statements. Note that using "Equals" in this case is not really correct; use == to compare numeric values.
Answer by Slobdell · Oct 06, 2013 at 03:48 PM
Change your random part to this
var random = Random.Range(0, 21);
You should be using Random.Range and putting the range in. Min is inclusive, max is exclusive per here: http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
This will return a whole number as well so you can omit the mathf.floor as well.