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 Bibi-Maghoo · Apr 12, 2014 at 09:43 PM · randomrange

Random range choosing both results?

I am trying to work out why when this script runs, it spawns both objects? Sounds silly, but I was trying to spawn one or the other, well, eventually many, but in this example just one. When it runs both objects appear.

 var randomNumber : int;
 var floorTileNorm : GameObject;
 
 function Start () {
 
 randomNumber = Random.Range(1, 2);
 
 if (randomNumber == 1);
 Instantiate (floorTileNorm, Vector3(100,200,100),Quaternion.identity);
 
 }
 
 if (randomNumber == 2);
 Instantiate (floorTileNorm, Vector3(400,200,100),Quaternion.identity);


Thanks for taking a look.

Comment
Add comment · Show 2
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 Bibi-Maghoo · Apr 12, 2014 at 10:43 PM 0
Share

Thank you both very much for the swift answer, which is correct. I have amended the code and it works.

Robertbu - I didn't use your code or an 'else' because I will be making the range much bigger, it is for use generating a 'random' floor tile position at the start of the game. It's something I literally decided to look at today. Could you please explain to me why the use of <0.5? If I understand what you are saying, then the last number is redundant, hence it would always be a '1'. But if the range is say 4, but I have 3 options, that would be fine right?

nicolasjr - thanks for that, your answer is also correct. I don't know how to make them both right answers :D

It works great now, time to write loads more options.

avatar image robertbu · Apr 12, 2014 at 11:53 PM 0
Share

Random.value returns a value between 0 and 1, so < 0.5 happens half the time. For many choices, you might want a case statement:

 switch (Random.Range(1,5) {
     case 1:
         //Do whatever for 1
         break;
     case 2:
         //Do whatever for 2
         break;
     case 3:
         //Do whatever for 3
         break;
     case 4:
         //Do whatever for 4
         break;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Apr 12, 2014 at 09:48 PM

You have two problems here. The first is the structure of your code. On line 8, you have a ';' at the end of your 'if' statement which terminates the 'if'. In addition, your '}' on line 11, closes off the 'Start()' function. The reason you get the second one is that your other code gets executed because of the way Javascript handles scripts in open code.

Another problem is that Random.Range() when used with integers is exclusive of the last number, therefore your code would only return 1...never 2. You may one something more like:

 #pragma strict
 
 var randomNumber : int;
 var floorTileNorm : GameObject;
  
 function Start () {
  
     if (Random.value < 0.5) {
         Instantiate (floorTileNorm, Vector3(100,200,100),Quaternion.identity);
     }
     else {
         Instantiate (floorTileNorm, Vector3(400,200,100),Quaternion.identity);
     }
 }
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
1

Answer by nicolasjr · Apr 12, 2014 at 09:48 PM

The problem is that you're using " ; " after the if statements, so, it's not interpreting that the above lines are about the statement.

So, it's really instantiating both objects. Just remove the " ; " from after if, and you'll be good to go. :)

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

22 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 avatar image avatar image avatar image

Related Questions

triggering random animations with gui 1 Answer

two Random.Range returning similar values 1 Answer

Random.Range changed in Unity 5? 2 Answers

Missing a Method? Random.Range? 1 Answer

Is it possible to grab an index in a List by random as in arrays? 2 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