- Home /
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);
}
}
}
}
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.
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
Follow this Question
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