Search Unity

simple level select menu loop

Discussion in 'Scripting' started by HarleyK, May 31, 2017.

  1. HarleyK

    HarleyK

    Joined:
    Mar 27, 2015
    Posts:
    95
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelSelect : MonoBehaviour {
    6.  
    7.     public GameObject[] levelImages;
    8.  
    9.     public int levelInt = 0;
    10.  
    11.     void Start()
    12.     {
    13.         levelImages[0].SetActive(true);
    14.         levelImages[1].SetActive(false);
    15.         levelImages[2].SetActive(false);
    16.         levelImages[3].SetActive(false);
    17.     }
    18.  
    19.  
    20.     public void RightArrow()
    21.     {
    22.         levelInt += 1;
    23.         if(levelInt >= 3)
    24.         {
    25.             levelInt = 3;
    26.         }
    27.         Panels(levelImages[levelInt]);
    28.     }
    29.  
    30.     public void LeftArrow()
    31.     {
    32.         levelInt -= 1;
    33.         if(levelInt <= 0)
    34.         {
    35.             levelInt = 0;
    36.         }
    37.         Panels(levelImages[levelInt]);
    38.     }
    39.  
    40.     public void Panels(GameObject panel)
    41.     {
    42.         for (int i = 0; i < levelImages.Length; i++)
    43.         {
    44.             if (levelImages[i] == panel)
    45.             {
    46.                 levelImages[i].SetActive(true);
    47.             }
    48.             else
    49.             {
    50.                 levelImages[i].SetActive(false);
    51.             }
    52.         }
    53.     }
    54. }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    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):
    1.  
    2. public GameObject[] levelImages;
    3.  
    4.     public int levelInt = 0;
    5.  
    6.     void Start()
    7.     {
    8.         Panels(0);
    9.     }
    10.  
    11.     public void RightArrow()
    12.     {
    13.         levelInt = levelInt >= levelImages.Length - 1 ? levelInt = levelImages.Length - 1 : levelInt += 1;
    14.        
    15.         Panels(levelInt);
    16.     }
    17.  
    18.     public void LeftArrow()
    19.     {
    20.         levelInt = levelInt <= 0 ? levelInt = 0 : levelInt -= 1;
    21.  
    22.         Panels(levelInt);
    23.     }
    24.  
    25.     public void Panels(int panel)
    26.     {
    27.         for (int i = 0; i < levelImages.Length; i++)
    28.         {
    29.             if(i == panel)
    30.             {
    31.                 levelImages[i].SetActive(true);
    32.                 continue;
    33.             }
    34.             levelImages[i].SetActive(false);
    35.         }
    36.     }
     
    Last edited: May 31, 2017
    HarleyK likes this.
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could even do:
    Code (csharp):
    1.  
    2. // as an example for left arrow.
    3. if (levelInt > 0) {
    4.    levelImages[levelInt].SetActive(false);
    5.    --levelInt;
    6.    levelImages[levelInt].SetActive(true);
    7. }
    8.  
    :)
    As for why it's not working, whichever way you choose, I'm not sure. Looks good to me, too.
     
    HarleyK likes this.
  4. HarleyK

    HarleyK

    Joined:
    Mar 27, 2015
    Posts:
    95
    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.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol. awesome (that you solved it, I mean) :)