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);
						
	

						}

			   
				}
		}
}

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.