- Home /
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.
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 ();
}
Oh no, xD
Thank you a lot. Sometimes the simplest failures causes the biggest errors.
Ich should read my codes more detailed.
Your answer
Follow this Question
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