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 floppyturtle32 · Sep 19, 2020 at 08:23 AM · 3dvariablerandomfloats

Random variable values?

Hello, is there any way to randomly assign a (float) variables values between a minimum and a max?

What I tried is to have a float like public float example = 20f and generate a random range in the start function, variablize it, and then instead do public float example = "variable"

Another user told me this for a similar project, and it worked, but when I apply it now, it says "variable" does not exist in the current context. This is probably because the public float is assigned before the random number is varablized, but when I try to move it after the start function, even more errors appear. Is there any code to work around this?

Thanks!

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
0

Answer by Bunny83 · Sep 14, 2020 at 02:23 AM

What the heck do you mean by "varablized"? A variable is a named piece of memory. Variables can not be created dynamically. Your example also doesn't make much sense. When you have a member variable like this:

 public float example = initialValue;

the field initializer (the = initialValue part) is executed before the constructor of the class actually runs and therefore long before any Start method might be called. Your next issue is that the field initializer is only used once for serialized fields- Since serialized fields get their value serialized in the inspector. So, once serialized, the value you assign in the field initializer becomes meaningless since it's overwritten by the serialized value anyways.


Why don't you just roll your random value in Start and assign it to the variable of your choice? In your case "example" IS your target variable.

Comment
Add comment · Show 7 · 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 floppyturtle32 · Sep 18, 2020 at 10:05 PM 0
Share

Hi, sorry for the late answer, and thanks for answering. I'm not sure what you mean about rolling my random value in start and assigning to my variable. What I'm doing right now is this,

public float degreesPerSecond = randomLocation;

     public float $$anonymous$$Spawn = 0;
     public float maxSpawn = 10;
 
     void Start()
     {      
         float randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
     }

Which yields the error: The name 'randomLocation' does not exist in the current context. Can you please elaborate about what you mean? Thanks.

avatar image Durstie floppyturtle32 · Sep 19, 2020 at 01:33 PM 1
Share

Try creating the randomLocation variable in the class instead of the Start() method.

  public float $$anonymous$$Spawn = 0;
  public float maxSpawn = 10;
  private float randomLocation;

Then, in start just set a value to it.

   randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);


Let me know if that helps.

avatar image floppyturtle32 Durstie · Sep 21, 2020 at 02:10 AM 0
Share

Thanks for the answer! However, when I apply the code you gave me, it still says "The name 'randomLocation' does not exist in the current context." From what I understand, the code you gave me is this:

 private float example = randomLocation;
 
      public float $$anonymous$$Spawn = 0;
      public float maxSpawn = 10;
  
      void Start()
      {      
          float randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
      }

I also tried this, in case that's what you meant:

   public float $$anonymous$$Spawn = 0;
     public float maxSpawn = 10;
     public float example = randomLocation;
     private float randomLocation;
  void Start()
     {
         
         float randomLocation = Random.Range($$anonymous$$Spawn, maxSpawn);
     }
 

But this yields the error A field initializer cannot reference the non-static field, method, or property 'Floater.randomLocation'

*note floater is just my script name.

Am I doing something wrong, if so what? Thanks for the help.

Show more comments
avatar image
0

Answer by UTus · Sep 24, 2020 at 07:45 AM

Get Random variable values

 var randomValue = Random.Range(min, max);


Get Random enum type

     public static T RandomEnum<T>()   
     {   
         Array values = Enum.GetValues(typeof(T));   
         return (T)values.GetValue(new System.Random().Next(0, values.Length));  
     }

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

191 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 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 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 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 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 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 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 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

Index out of range for no reason? 1 Answer

Creating a class? 2 Answers

how do you access and use a variable from a separate script then run an if statement depending on the value of that variable? 0 Answers

Creating an animation with variables? 2 Answers

Spawn Random Enemy 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