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 Nynex71 · Jan 02, 2011 at 06:26 AM · guierror

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

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
1
Best Answer

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 }

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 Nynex71 · Jan 02, 2011 at 08:37 AM 0
Share

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.

avatar image
0

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.

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 Nynex71 · Jan 02, 2011 at 09:26 AM 0
Share

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

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

No one has followed this question yet.

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


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