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 Arksol · Jul 19, 2014 at 09:43 AM · c#guivariablesinventorycursor

Inventory Cursor Location

Hello,

currently I try to make a little "inventory script". If I press the "F" button, the game stops and 4 "buttons" appear and around the first button a cursor.

If the D button is pressed the cursor shall move 1 button to the right and if A is pressed one button to the left.

But somehow if I press D, it jumps from button number 1 to button number 4. But backwards it works perfectly 4-3-2-1.

So my question is... why does the cursor jumps from 1 to 4 ? ^^;

 using UnityEngine;
 using System.Collections;
 
 public class InventoryController : MonoBehaviour {
     //Item Anzeigen
     public Texture item1;
     public Texture item2;
     public Texture item3;
     public Texture item4;
 
     //Ausgewählte Items
     public Texture auswahl1;
     public Texture auswahl2;
 
     public Texture cursor;
     private bool inventarAn;
     private float cursorLocation;
 
 
     void Start (){
         cursorLocation = 1.1f;
 
         }
         
 
     void Update ()
     {
                 if (Input.GetKeyDown ("f")) {
                         Debug.Log ("F gedrückt !");
                         if (inventarAn == false) {
                                 Time.timeScale = 0;
                                 inventarAn = true;
                                 
                         
                         } else {
 
                                 inventarAn = false;
                                 cursorLocation = 1.1f;
                                 Time.timeScale = 1;
                                 Debug.Log ("F aus !");
                 
                         }
                 }
 
                 if (inventarAn == true) {
                         if (cursorLocation == 1.1f) {
                                 if (Input.GetKeyDown ("d")) {
                                         cursorLocation = 1.2f; 
                                         Debug.Log ("1.2");
                                 }                                                       
                         }
                         if (cursorLocation == 1.2f) {
                                 if (Input.GetKeyDown ("d")) {
                                         cursorLocation = 1.3f;
                                         Debug.Log ("1.3");
                                 }
                                 if (Input.GetKeyDown ("a")) {
                                         cursorLocation = 1.1f;
                                         Debug.Log ("1.1");
                                 }
                                 
                          
                         }
                         if (cursorLocation == 1.3f) {
                                 if (Input.GetKeyDown ("a")) {
                                         cursorLocation = 1.2f; 
                                         Debug.Log ("1.2");
                         
                                 }
                                 if (Input.GetKeyDown ("d")) {
                                         cursorLocation = 1.4f; 
                                         Debug.Log ("1.4");
                                 }
                     
                                 
                         }
                         if (cursorLocation == 1.4f) {
                                 if (Input.GetKeyDown ("a")) {
                                         cursorLocation = 1.3f; 
                                         Debug.Log ("1.3");
                                 }                                
                         }
                 }
         }
 
     
             
 
     void OnGUI ()
     {
                 if (inventarAn == true) {        
                         // Inventar Grafiken werden ausgegeben
                         GUI.DrawTexture (new Rect  (20, 20, 100, 100), item1, ScaleMode.ScaleToFit);
                         GUI.DrawTexture (new Rect (120, 20, 100, 100), item2, ScaleMode.ScaleToFit);
                         GUI.DrawTexture (new Rect (220, 20, 100, 100), item3, ScaleMode.ScaleToFit);
                         GUI.DrawTexture (new Rect (320, 20, 100, 100), item4, ScaleMode.ScaleToFit);
                         //Cursor Grafik
                         if (cursorLocation == 1.1f) {
                                 GUI.DrawTexture (new Rect (20, 20, 100, 100), cursor, ScaleMode.ScaleToFit);
                         }
                         if (cursorLocation == 1.2f) {
                                 GUI.DrawTexture (new Rect (120, 20, 100, 100), cursor, ScaleMode.ScaleToFit);
                         }
                         if (cursorLocation == 1.3f) {
                                 GUI.DrawTexture (new Rect (220, 20, 100, 100), cursor, ScaleMode.ScaleToFit);
                         }
                         if (cursorLocation == 1.4f) {
                                 GUI.DrawTexture (new Rect (320, 20, 100, 100), cursor, ScaleMode.ScaleToFit);
                         
     
 
                         }
 
                
                 }
         }
 }





Comment
Add comment · Show 1
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 Arksol · Jul 19, 2014 at 06:45 PM 0
Share

Nobody knows how to fix that? :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Immanuel-Scholz · Jul 19, 2014 at 06:58 PM

The problem is, that your second, third and fourth "if" clause come directly after the first one and are evaluated in the same frame.

So imagine the cursor is on the first button, and the user presses D. The program jumps into the first test "if (cursorLocation == 1.1f)" which is true and sets the cursorLocation to 1.2. But then, after it finishes the if-clause, it evaluates immediately the second if-clause "if (cursorLocation == 1.2f)" which JUST became true!

What you want is use if/else if/else if chains.

 if (cursorLocation == 1.1f)
 ...
 else if (cursorLocation == 1.2f)
 ...
 else if (cursorLocation == 1.3f)
 ...

Grüße aus München, Imi.

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 Arksol · Jul 19, 2014 at 09:47 PM 0
Share

Hi,

thanks alot for your comment. I'll try to use those if/else chains asap.

But why does the script react when it jumps from 1.1 to 1.2? First it should check "if (cursorLocation == 1.2f)" is true but right after that it should check if the D button is pressed and just then switch the variable to 1.3. And...also I wonder why it works backwards.

So..I start at 1.1 press D it jumps to 1.4 Then I press A and it moves to 1.3, I press A again and it moves to 1.2, A again and it moves to 1.1 .

What is the difference in the code between the way from 1.1 to 1.4 and the way from 1.4 to 1.1 ?

I...i cant understand / see the difference :(

Danke für die Hilfe und beste Grüße aus Hamburg ~$$anonymous$$arcel

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI.Button is acting funky. 2 Answers

Multiple Cars not working 1 Answer

Textfield cursor is missing. 1 Answer

Help with GUIText health bar please 1 Answer

Is there any easy way to keep buttons inside of box? (c#) 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