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 Duban · Jun 23, 2015 at 12:57 PM · c#guibuttonrandom

C# Random Number on GUIButton click without updating every frame

Hey Guys,

I am new to Unity and learning C# for nearly 2 month and have a little problem. I try to build a statmenu where the player can roll a dice via GUI.Button to get random stats, but the are changing every frame until you left the menu.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public bool StatsSelectionWindow;
 
 public int statsleft;
 
     int diceStrength;
     int diceDexterity;
     int dicePersistence;
     int diceResistence;
     int diceTactics;
     int diceAccuracy;
     int diceCharisma;
     int diceGrasp;
 
 public GameObject Player;
 
     public void DiceStats (){
 
         GameObject Player = GameObject.Find("Player");
         UserStats userStats = Player.GetComponent<UserStats>();
         
         diceStrength = Random.Range (2, 5) - Random.Range (0, 3);
         diceDexterity = Random.Range (2, 5) - Random.Range (0, 3);
         dicePersistence = Random.Range (2, 5) - Random.Range (0, 3);
         diceResistence = Random.Range (2, 5) - Random.Range (0, 3);
         diceTactics = Random.Range (2, 5) - Random.Range (0, 3);
         diceAccuracy = Random.Range (2, 5) - Random.Range (0, 3);
         diceCharisma = Random.Range (2, 5) - Random.Range (0, 3);
         diceGrasp = Random.Range (2, 5) - Random.Range (0, 3);
         
         userStats.strength = userStats.minStrength + diceStrength;
         userStats.dexterity = userStats.minDexterity + diceDexterity;
         userStats.persistence = userStats.minPersistence + dicePersistence;
         userStats.resistence = userStats.minResistence + diceResistence;
         userStats.tactics = userStats.minTactics + diceTactics;
         userStats.accuracy = userStats.minAccuracy + diceAccuracy;
         userStats.charisma = userStats.minCharisma + diceCharisma;
         userStats.grasp = userStats.minGrasp + diceGrasp;
 
 }
 
 void OnGUI () {
 
 if (StatsSelectionWindow) {
 
             GUI.Box(new Rect(0,0, Screen.width, Screen.height), "Choose stats");
             GUI.Box(new Rect(0,0, 400, 270), "");
             
             GameObject Player = GameObject.Find("Player");
             UserStats userStats = Player.GetComponent<UserStats>();
 
 // Button for new random stats
             if (GUI.Button (new Rect (250, 240,125, 22), "roll stats new"));{
 
                         DiceStats ();
             }
 
             GUI.Label (new Rect (10, 200, 400, 22), "_____________________________________________________");
             GUI.Label (new Rect ( 150, 220, 250, 22), "Stats left\t    " + statsleft.ToString());
 
             // Label for min., max. and cur. stats
             GUI.Label (new Rect (10, 10, 450, 22), "Attributte \t\t\tMin. \tAktuell \t    Max.");
             GUI.Label (new Rect (10, 50, 450, 22), userStats.nameStrength);
             GUI.Label (new Rect (205, 50, 50, 22), userStats.minStrength.ToString() );
             if (GUI.Button (new Rect (235, 50 , 25, 22), "<") && userStats.strength > userStats.minStrength){
                 userStats.strength --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 50, 450, 22), userStats.strength.ToString() );
             if (GUI.Button (new Rect (305, 50 , 25, 22), ">") && userStats.strength < userStats.maxStrength && statsleft > 0){
                 userStats.strength ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 50, 450, 22), userStats.maxStrength.ToString() );
             GUI.Label (new Rect (10, 70, 450, 22), userStats.nameDexterity);
             GUI.Label (new Rect (205, 70, 50, 22), userStats.minDexterity.ToString() );
             if (GUI.Button (new Rect (235, 70 , 25, 22), "<") && userStats.dexterity > userStats.minDexterity){
                 userStats.dexterity --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 70, 450, 22), userStats.dexterity.ToString() );
             if (GUI.Button (new Rect (305, 70 , 25, 22), ">") && userStats.dexterity < userStats.maxDexterity && statsleft > 0){
                 userStats.dexterity ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 70, 450, 22), userStats.maxDexterity.ToString() );
             GUI.Label (new Rect (10, 90, 450, 22), userStats.namePersistence);
             GUI.Label (new Rect (205, 90, 50, 22), userStats.minPersistence.ToString() );
             if (GUI.Button (new Rect (235, 90 , 25, 22), "<") && userStats.persistence > userStats.minPersistence){
                 userStats.persistence --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 90, 450, 22), userStats.persistence.ToString() );
             if (GUI.Button (new Rect (305, 90 , 25, 22), ">") && userStats.persistence < userStats.maxPersistence && statsleft > 0){
                 userStats.persistence ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 90, 450, 22), userStats.maxPersistence.ToString() );
             GUI.Label (new Rect (10, 110, 450, 22), userStats.nameResistence);
             GUI.Label (new Rect (205, 110, 50, 22), userStats.minResistence.ToString() );
             if (GUI.Button (new Rect (235, 110 , 25, 22), "<") && userStats.resistence > userStats.minResistence){
                 userStats.resistence --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 110, 450, 22), userStats.resistence.ToString() );
             if (GUI.Button (new Rect (305, 110 , 25, 22), ">") && userStats.resistence < userStats.maxResistence && statsleft > 0){
                 userStats.resistence ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 110, 450, 22), userStats.maxResistence.ToString() );
             GUI.Label (new Rect (10, 130, 450, 22), userStats.nameTactics);
             GUI.Label (new Rect (205, 130, 50, 22), userStats.minTactics.ToString() );
             if (GUI.Button (new Rect (235, 130 , 25, 22), "<") && userStats.tactics > userStats.minTactics){
                 userStats.tactics --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 130, 450, 22), userStats.tactics.ToString() );
             if (GUI.Button (new Rect (305, 130 , 25, 22), ">") && userStats.tactics < userStats.maxTactics && statsleft > 0){
                 userStats.resistence ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 130, 450, 22), userStats.maxTactics.ToString() );
             GUI.Label (new Rect (10, 150, 450, 22), userStats.nameAccuracy);
             GUI.Label (new Rect (205, 150, 50, 22), userStats.minAccuracy.ToString() );
             if (GUI.Button (new Rect (235, 150 , 25, 22), "<") && userStats.accuracy > userStats.minAccuracy){
                 userStats.accuracy --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 150, 450, 22), userStats.accuracy.ToString() );
             if (GUI.Button (new Rect (305, 150 , 25, 22), ">") && userStats.accuracy < userStats.maxAccuracy && statsleft > 0){
                 userStats.accuracy ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 150, 450, 22), userStats.maxAccuracy.ToString() );
             GUI.Label (new Rect (10, 170, 450, 22), userStats.nameCharisma);
             GUI.Label (new Rect (205, 170, 50, 22), userStats.minCharisma.ToString() );
             if (GUI.Button (new Rect (235, 170 , 25, 22), "<") && userStats.charisma > userStats.minCharisma){
                 userStats.charisma --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 170, 450, 22), userStats.charisma.ToString() );
             if (GUI.Button (new Rect (305, 170 , 25, 22), ">") && userStats.charisma < userStats.maxCharisma && statsleft > 0){
                 userStats.charisma ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 170, 450, 22), userStats.maxCharisma.ToString() );
             GUI.Label (new Rect (10, 190, 450, 22), userStats.nameGrasp);
             GUI.Label (new Rect (205, 190, 50, 22), userStats.minGrasp.ToString() );
             if (GUI.Button (new Rect (235, 190 , 25, 22), "<") && userStats.grasp > userStats.minGrasp){
                 userStats.grasp --;
                 statsleft ++;
             }
             GUI.Label (new Rect (285, 190, 450, 22), userStats.grasp.ToString() );
             if (GUI.Button (new Rect (305, 190 , 25, 22), ">") && userStats.grasp < userStats.maxGrasp && statsleft > 0){
                 userStats.grasp ++;
                 statsleft --;
             }
             GUI.Label (new Rect (350, 190, 450, 22), userStats.maxGrasp.ToString() );
 
             // Button to get back to name
             if (GUI.Button (new Rect (Screen.width / 2 + 150, Screen.height / 2 + 250, 125, 40), "Zurück")){
                 statsleft = 0;
                 StatsSelectionWindow = false;
                 NameInputWindow = true;
             }
             
             // Button to move to skills
             if (GUI.Button (new Rect (Screen.width / 2 + 280, Screen.height / 2 + 250, 125, 40), "Weiter")){
                 StatsSelectionWindow = false;
                 SkillSelectionWindow = true;
             }
         }

I hope this code is readable and sorry for my bad language, it is not my nativ.

Thanks for helping.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Landern · Jun 23, 2015 at 12:59 PM

Line:55 you have a semi-colon terminating the if statement :(

      if (GUI.Button (new Rect (250, 240,125, 22), "roll stats new"));{
 
                  DiceStats ();
      }

should be:

      if (GUI.Button (new Rect (250, 240,125, 22), "roll stats new")) {
 
                  DiceStats ();
      }
Comment
Add comment · Show 1 · 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 Duban · Jun 23, 2015 at 03:06 PM 0
Share

Oh no, xD

Thank you a lot. Sometimes the simplest failures causes the biggest errors.

Ich should read my codes more detailed.

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

2 People are following this question.

avatar image avatar image

Related Questions

How Do You Use GUIText to Move Through Multiple Texts? 1 Answer

Problem with multi-touch on android game 1 Answer

Generating tooltips for multiple buttons. 1 Answer

How to trigger button press on a button in an editor window by pressing the return key ? 0 Answers

Distribute terrain in zones 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