Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to change GameObject to another using a slider?

Discussion in 'AR/VR (XR) Discussion' started by clehouede, May 1, 2019.

  1. clehouede

    clehouede

    Joined:
    Feb 25, 2019
    Posts:
    1
    I want to use a UI slider in Unity for my AR project. I am trying to modify my gameobject (and a text that goes with each of them) everytime the slider hits a new value. I have 3 gameobjects in total.

    Example:
    When slider hits value 0, "Seed"(text) should appear, and "Seed" (as a game object) should appear.
    When slider hits value 1, "Seed"(text) should change to text "Sprout", and "Sprout" (as a game object) should appear (replacing "Seed" GameObject)
    When slider hits value 2, "Sprout"(text) should change to text "Flower", and "Flower" (as a game object) should appear (replacing "Sprout" GameObject)

    Thank you!
     
  2. sfjohansson

    sfjohansson

    Joined:
    Mar 12, 2013
    Posts:
    369
    You can set up the slider to cover the range you need ie 0,1,2 with stepped increments.

    Then have a script that controls which game objects are active when the value on the slider changes. On this script you setup the gameobjects in an array/list where after you can use a for-loop to iterate through the objects...so something like:

    Code (CSharp):
    1.  
    2. for (i=0;i<listOfThings.Count;i++)
    3. {
    4.     listOfThings[i].SetActive(i==sliderValue);
    5. }
    6.