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)