So after a late night of work, I start working on a very simple menu loop, the player taps on an arrow and it loops through an array of images one at a time. The right arrow works fine, it loops through each one the way its suppose to, however the left arrow won't go backwards through the loop. I figured maybe i just needed to make a loop that decreases the int to make it loop back down but i tried that and it didn't work out. I'm tired and most likely am just over thinking it but here is the code. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class LevelSelect : MonoBehaviour { public GameObject[] levelImages; public int levelInt = 0; void Start() { levelImages[0].SetActive(true); levelImages[1].SetActive(false); levelImages[2].SetActive(false); levelImages[3].SetActive(false); } public void RightArrow() { levelInt += 1; if(levelInt >= 3) { levelInt = 3; } Panels(levelImages[levelInt]); } public void LeftArrow() { levelInt -= 1; if(levelInt <= 0) { levelInt = 0; } Panels(levelImages[levelInt]); } public void Panels(GameObject panel) { for (int i = 0; i < levelImages.Length; i++) { if (levelImages[i] == panel) { levelImages[i].SetActive(true); } else { levelImages[i].SetActive(false); } } } }
I don't see any reason why it wouldn't work, are you calling the left arrow method? Although, I would of done it differently myself and just compared the int values; Code (CSharp): public GameObject[] levelImages; public int levelInt = 0; void Start() { Panels(0); } public void RightArrow() { levelInt = levelInt >= levelImages.Length - 1 ? levelInt = levelImages.Length - 1 : levelInt += 1; Panels(levelInt); } public void LeftArrow() { levelInt = levelInt <= 0 ? levelInt = 0 : levelInt -= 1; Panels(levelInt); } public void Panels(int panel) { for (int i = 0; i < levelImages.Length; i++) { if(i == panel) { levelImages[i].SetActive(true); continue; } levelImages[i].SetActive(false); } }
You could even do: Code (csharp): // as an example for left arrow. if (levelInt > 0) { levelImages[levelInt].SetActive(false); --levelInt; levelImages[levelInt].SetActive(true); } As for why it's not working, whichever way you choose, I'm not sure. Looks good to me, too.
thank you both for the responses, apparently it wasn't the coding at all, one of my friends who used the arrows, flipped the ui sprite on the y so it wouldn't take inputs.