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 Gilead7 · Mar 29, 2012 at 09:47 PM · c#random.rangeongui

random not working

I am attempting to create a character generator script. I'm making private variables to grab the names from the enum list. That is working, but when I try to define a public variable with a random int and place it in the OnGUI, it doesn't like it whether I put the definitions in awake or start.

Here is the code:

 using UnityEngine;
 using System.Collections;
 using System; 
 
 public class CharacterGenerator : MonoBehaviour
 {
     public const float VERSION=.1f;
     public bool ClearPrefs=false;
     private string _constitution= Attributes.Stat.Constitution.ToString();
     private string _strength= Attributes.Stat.Strength.ToString();
     private string _stamina= Attributes.Stat.Stamina.ToString();    
     private string _dexterity= Attributes.Stat.Dexterity.ToString();        
     private string _intelligence= Attributes.Stat.Intelligence.ToString();    
     private string _wisdom= Attributes.Stat.Wisdom.ToString();
     private string _focus= Attributes.Stat.Focus.ToString();
 
     // Use this for initialization
     void Start ()
     {
         public Constitution=Random.Range(1, 20); //error here with red line    
         //public int Strength= UnityEngine.Random.Range(1, 20);
         //public int Stamina= UnityEngine.Random.Range(1, 20);
         //public int Dexterity= UnityEngine.Random.Range(1, 20);
         //public int Intelligence= UnityEngine.Random.Range(1, 20);
         //public int Wisdom= UnityEngine.Random.Range(1, 20);
         //public int Focus= UnityEngine.Random.Range(1, 20);
     }

     // Update is called once per frame
     void Update ()
     {
         public Constitution=Random.Range(1, 20); here too
     }
     
     void GenerateStats()
     {
         GUI.Label (new Rect (40, 50, 700, 50), _constitution + ":" + Constitution ); 
         GUI.Label (new Rect (40, 70, 700, 50), _strength + ":" + Strength);
         GUI.Label (new Rect (40, 90, 700, 50), _stamina + ":" + Stamina);
         GUI.Label (new Rect (40, 110, 700, 50), _dexterity + ":" + Dexterity);
         GUI.Label (new Rect (40, 130, 700, 50), _intelligence + ":" + Intelligence);
         GUI.Label (new Rect (40, 150, 700, 50), _wisdom + ":" + Wisdom);
         GUI.Label (new Rect (40, 170, 700, 50), _focus + ":" + Focus);
     }
 
     void OnGUI()
     {
         GUI.Label (new Rect (Screen.width/2, 10, 700, 50), "True Calling Character Generator!");
         if(GUI.Button (new Rect (250, 300, 100, 50), "Generate Stats")){
             Debug.Log("Generate our Stats!");
             GenerateStats();
         }
         if (GUI.Button (new Rect (355, 300, 100, 50), "Start Game")){
             Debug.Log("Start Our Game!");
         }
     }
 }

Only the OnGUI stuff is showing up. It worked once when everything was in the OnGUI function, but when I tried it again, I got an exception and zero values. What am I doing, not doing, should be doing? Thanks!

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 DaveA · Mar 30, 2012 at 12:18 AM 0
Share

One thing you should be doing: edit your question, select your code, hit the 010/101 button to format it

avatar image Gilead7 · Apr 02, 2012 at 07:53 PM 0
Share

Thanks! I didn't even know that was there.

2 Replies

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

Answer by xellow · Mar 30, 2012 at 12:40 AM

Constitution needs to be declared as an int.

Your code should be

 public int Constitution=(int)Random.Range(1,20);

Random.Range should also be casted to an int because that method returns a float.

Also you can't declare a public variable inside a function. Local variables are automatically private.

And finally, a good code practice is to use camelcase for variable names and only start with a capital letter if you're referring to a class.

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 DaveA · Mar 30, 2012 at 12:42 AM 0
Share

$$anonymous$$inda what I said

avatar image xellow · Mar 30, 2012 at 01:03 AM 2
Share

$$anonymous$$y post had to be approved by a moderator and there weren't any answers yet when I wrote up my response. Sorry for the repeat.

avatar image
1

Answer by DaveA · Mar 30, 2012 at 12:20 AM

public int Constituion (or public float Constitution) for one. Also put that public int Constitution (that's called a 'declaration') outside of the other functions at the top (but inside the class). Like right above Start. But set the value in Start and Update, don't bother assigning it a random value in the declaration.

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 Gilead7 · Apr 02, 2012 at 07:55 PM 0
Share

I had originally placed it there, but the editor suggested placing it in update or start, but it didn't work there either.

Now it's saying random.range can only be called from the main thread. What does that mean?

avatar image Bunny83 · Apr 02, 2012 at 08:40 PM 1
Share

No, you interpreted the error the wrong way. It was telling you that you can use a function (in this case Random.Range) as a field-initializer. Only constant values can be used there. So it suggested to initialize your variable in Start, but not to remove the class variable.

 public class CharacterGenerator : $$anonymous$$onoBehaviour
 {
     // [...]
     public int Constitution;  // variable declaration
 
     void Start ()
     {
         Constitution = Random.Range(1, 20); // variable initialization

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Randomly Select String from Array 0 Answers

What is the best way to grab and move the immediate children of a GameObject. 1 Answer

Random.Range, GameObjects and Length 1 Answer


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