Search Unity

Find current index of an array

Discussion in 'Scripting' started by nieyoub23, May 16, 2017.

  1. nieyoub23

    nieyoub23

    Joined:
    May 8, 2016
    Posts:
    27
    I have a slider and an array of Gameobjects. When the slider changes, I want to disable the current gameObject and enable the corresponding gameObject.
    I take the value of the slider and use it to enable the new gameObject but I dont know how to get the value of the current element array that is active. I can only find answers of how to get the value of the element itself, not the element index.
    Ive been trying to also set that value to a new variable and use a few different things but I cant figure it out.
    My code looks something like this.

    public GameObject[] graphPoint;

    public void UserSlide(){

    int x=(int)sliderval.value;
    graphPoint[x].SetActive(true);

    }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    you can either store the value in a global scope int each time you change it or you can use a for loop to disable every object in the array with the exception of the current x value.
     
    nieyoub23 likes this.