Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fortunez · Oct 06, 2013 at 02:56 PM · javascripterrorrandomitem

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)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FrankStarsKo · Oct 06, 2013 at 03:18 PM 0
Share

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"

avatar image TrickyHandz · Oct 06, 2013 at 03:41 PM 0
Share

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.

avatar image Eric5h5 · Oct 06, 2013 at 03:49 PM 0
Share

The question isn't about running it more than once; that's a separate issue.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

can your Random.Range parameters match with function perameter (javascript) 2 Answers

Random Range BUT only on specific Co Ords 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Random number that doesn't repeat. (javascript) 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges