- Home /
My script has no errors but won't run correctly...
I have a script and i get no errors or warnings but when i run it but it still wont run correctly. I set up the input tags but still nothing. I don't know if i'm even allowed to try to do what i'm running but i don't see why not. I used to have it two different script but because it has to modify stuff that is contained on the other script i don't want to have to deal with special variables. But once again i want to be able to open up a gui with the push of a button... And btw if you know can you tell be how i can make a default job... As in the first time you join the game you are set to a citizen... I think its already applied but there isnt a script to make sure. thanks in advanced.
Here is the script i have now...
using UnityEngine; using System.Collections;
public class JobInfo : MonoBehaviour { public int curMoney = 1000; public int curHealth = 100; public int maxHealth = 100; public string curJob; public int Salary = 50; public int payTime = 180;
public float healthBarLength;
void Start () {
InvokeRepeating ("AddMoney", payTime, payTime);
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
}
void OnGUI() {
Jobs();
GUI.Label (new Rect (10, 10, 100, 30), "Job: ");
GUI.Label (new Rect (35, 10, 100, 30), curJob);
GUI.Label (new Rect (80, 10, 100, 30), "| Salary: ");
GUI.Label (new Rect (130, 10, 100, 30), Salary.ToString());
GUI.Label (new Rect (150, 10, 100, 30), "| Money: ");
GUI.Label (new Rect (200, 10, 100, 30), curMoney.ToString());
GUI.Box(new Rect(10, Screen.height - 30, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AdjustCurrentHealth(int adj) {
curHealth +=adj;
if(curHealth < 1)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
void AddMoney ()
{
curMoney += Salary;
}
public void Jobs() {
if(Input.GetButtonDown("Jobs"))
{ if (GUI.Button (new Rect (25, 25, 100, 30), "Citizen")) { } if (GUI.Button (new Rect (25, 50, 100, 30), "Salesman")) { Salesman(); } if (GUI.Button (new Rect (25, 75, 100, 30), "Cook")) { } if (GUI.Button (new Rect (25, 100, 100, 30), "Hitman")) { // This code is executed when the Button is clicked } if (GUI.Button (new Rect (100, 25, 100, 30), "S.W.A.T.")) { // This code is executed when the Button is clicked } if (GUI.Button (new Rect (100, 50, 100, 30), "Police")) { // This code is executed when the Button is clicked } if (GUI.Button (new Rect (100, 75, 100, 30), "Doctor")) { // This code is executed when the Button is clicked } if (GUI.Button (new Rect (100, 100, 100, 30), "Mayor")) { // This code is executed when the Button is clicked } } }
public void Salesman () { if (GUI.Button (new Rect (25, 50, 100, 30), "Gun Salesman")) { // This code is executed when the Button is clicked } if (GUI.Button (new Rect (25, 50, 100, 30), "Car Salesman")) { // This code is executed when the Button is clicked } if (GUI.Button (new Rect (25, 50, 100, 30), "Drug Salesman")) { // This code is executed when the Button is clicked } }
}
But this is the part that i want you to look at
public void Jobs() {
if(Input.GetButtonDown("Jobs"))
{
if (GUI.Button (new Rect (25, 25, 100, 30), "Citizen")) {
}
if (GUI.Button (new Rect (25, 50, 100, 30), "Salesman")) {
Salesman();
}
if (GUI.Button (new Rect (25, 75, 100, 30), "Cook")) {
}
if (GUI.Button (new Rect (25, 100, 100, 30), "Hitman")) {
// This code is executed when the Button is clicked
}
if (GUI.Button (new Rect (100, 25, 100, 30), "S.W.A.T.")) {
// This code is executed when the Button is clicked
}
if (GUI.Button (new Rect (100, 50, 100, 30), "Police")) {
// This code is executed when the Button is clicked
}
if (GUI.Button (new Rect (100, 75, 100, 30), "Doctor")) {
// This code is executed when the Button is clicked
}
if (GUI.Button (new Rect (100, 100, 100, 30), "Mayor")) {
// This code is executed when the Button is clicked
}
}
}
public void Salesman () {
if (GUI.Button (new Rect (25, 50, 100, 30), "Gun Salesman")) {
// This code is executed when the Button is clicked
}
if (GUI.Button (new Rect (25, 50, 100, 30), "Car Salesman")) {
// This code is executed when the Button is clicked
}
if (GUI.Button (new Rect (25, 50, 100, 30), "Drug Salesman")) {
// This code is executed when the Button is clicked
}
}
Answer by Tetrad · Jan 02, 2011 at 07:58 AM
Salesman()
will only get called one frame, and that's when you release the mouse button after clicking the "Salesman" button.
What you need to do is set up some kind of state system. The easiest way to do this is set up some kind of enum for what menu you want open and do a simple switch
/case
branch on a member variable that you set from your UI elements.
As a naive example:
void OnGUI() { switch( currentState ) { case MenuState.JobsMenu: Jobs(); break; case MenuState.SalesmanMenu: Salesman(); break; // etc }
void Jobs() { // stuff if( GUI.Button( "Salesman" ) ) // obviously simplified { currentState = MenuState.SalesmanMenu; } // etc }
thanks, yeah i played around with the script you gave me and it actually worked... Coding for my game is practically done now i just need to learn to be artistic.
Answer by YoshieMaster · Jan 02, 2011 at 09:22 AM
Ah, you have stumbled on the annoying logic error. These are very annoying because they are difficult to solve. I would recommend adding heaps of Debug.Log statements.
i might try that soon.... for a strange reason i tried tetrad solution about and it worked on the first debug but all the rest it failed... doesn't make much sense... if a script works the first time and isn't modified than it should work the second.
Your answer
Follow this Question
Related Questions
Can't draw a box using Basic GUI 1 Answer
Loading a scene in game with a GUI 1 Answer
Instantiating object from inventory 0 Answers
How do I change the text of a gui image text 1 Answer
Errors when adding a GUI.Label C# 2 Answers