Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to Access Slider through code

Discussion in 'Scripting' started by EliJNemer, Oct 5, 2019.

  1. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    hi,

    i am working on a game where on start everything is instantiated.

    one of my objects is a slider should be connected to a script on one of the objects.

    on start i want to place the slider from the Main Camera(Clone) into the Slider slider of the script of the game object which requires the sliders use.

    also i need to make the slider function access this script. i need to set the game object in the slider and choose the function. which i do not know how to do through code..

    i know how to do this if everything is already in the scene but they are being instantiated instead so the values are null....
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    When you instantiate a GameObject you will receive a reference to that newly-created GameObject.

    For instance, if this is your prefab:

    Code (csharp):
    1. public GameObject MyPrefab;
    then later in your code when you instantiate it:

    Code (csharp):
    1. GameObject go = Instantiate<GameObject>( MyPrefab);
    At that point the variable called 'go' points at that new instance. I recommend a better variable name than 'go' obviously.

    Now you can use go.GetComponent<Slider>() to find the Slider on that GameObject, then assign it to another script:

    Code (csharp):
    1. Slider slider = go.GetComponent<Slider>();
     
  3. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    hey thanks for the reply,

    ive been trying allot to get the slider.. its just not working.. i have done it exactly as you wrote but it is not filling Slider slider..

    the canvas which holds the slider is a child of the mainCamera.. does that make a difference?

    i wrote the code: i was able to retrieve the mainCamera GameObject, then in a different variable Slider slider i wrote:
    slider = mainCamera.GetComponent<Slider>();

    still did nothing..
     
  4. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    i was finally able to retrieve the slider using the code:

    Code (CSharp):
    1. slider = (Slider)FindObjectOfType(typeof(Slider));
    not i just need to add the function to the slider.. add the gameobject then set function to the dynamic slider control..
     
    unity_FUc_6LWWe7c3mw likes this.
  5. EliJNemer

    EliJNemer

    Joined:
    Aug 3, 2019
    Posts:
    123
    1. Code (CSharp):
      1.  slider.onValueChanged.AddListener(ListenerMethod);
      2. public void ListenerMethod(float value)
      3. {
      4. }
     
  6. MrW82

    MrW82

    Joined:
    Feb 2, 2022
    Posts:
    4
    Took me a long time to figure out why I couldnt access the Slider through code.
    Apparently I had been trying to use

    "using UnityEngine.UIElements"
    instead of
    "using UnityEngine.UI".

    Changing that solved all my problems.
     
    abitowhit and LordVise like this.