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 caleb_b · Jul 28, 2014 at 05:12 PM · randomspeedfloatvalue

Questions about a random value between two numbers

Okay, basically I have three questions.

  1. Say I want to make a "speed" variable of type "float" to be a value between 10 and 15. What is the formatting for this?

  2. If the script with this variable were attached to a prefab, would every instance of that prefab have a different "speed" value?

  3. How would this randomization work? Like, what values would be selected? Would it just choose 10, 11, 12, 13, 14, and 15? Or would there be 10.5, 11, 11.5, 12, etc? Or would it go into even smaller numbers? Would there be a value of, say, 14.56783429?

Hopefully this is clear enough. Let me know if I need to clarify anything.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Jul 28, 2014 at 05:16 PM

  1. var speed = Random.Range (10.0, 15.0);

  2. Yes.

  3. It chooses any float value between 10.0 and 15.0. If you want whole numbers, then use integers instead of floats (in which case the upper value is exclusive; look up Random.Range in the docs).

Comment
Add comment · Show 2 · 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 caleb_b · Jul 28, 2014 at 05:33 PM 0
Share

Thanks for the quick response! Your formatting makes sense to me, but Unity doesn't like it. Here is my simple script for moving a gameobject.

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$over : $$anonymous$$onoBehaviour
 {
     public float speed = Random.Range(10.0, 15.0);
 
     void Start ()
     {
         rigidbody.velocity = transform.forward * speed;
     }
 }

Resulting in

Assets/Scripts/$$anonymous$$over.cs(6,37): error CS1502: The best overloaded method match for UnityEngine.Random.Range(float, float)' has some invalid arguments and Assets/Scripts/$$anonymous$$over.cs(6,37): error CS1503: Argument #1' cannot convert double' expression to type float'

avatar image meat5000 ♦ · Jul 28, 2014 at 05:40 PM 3
Share

Stick an f after the numbers

 10.0f

avatar image
0

Answer by _ADN_ · Jul 28, 2014 at 05:28 PM

1 - Random.Range : http://docs.unity3d.com/ScriptReference/Random.Range.html

2 - Yes, umless you make a static variable and just access it in every monovehabiour.

3 - Will be xx,xxxxx. If you want, you can round to decimals, example:

2 decimals Mathf.Round(yourFloat * 100f) / 100f; in this case value will be x.xx;

3 decimals Mathf.Round(yourFloat * 1000f) / 1000f; in this case value will be x.xxx;

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

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

Related Questions

Make String Update for Value (GUI) 1 Answer

zero is not zero 2 Answers

Random value for if statement 2 Answers

Assign a random value to var personality" upon asset creation (Java) 1 Answer

Slider value not working? 0 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