Search Unity

Dynamically update position-value of GUI based slider

Discussion in 'Scripting' started by eco_bach, Feb 26, 2020.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Working backwards sort of but I need to update the value-position of a GUI slider dynamically.
    This is the code used to create my slider. How would I dynamically update this slider AFTER it has been created inside OnGUI?
    Code (csharp):
    1.  
    2.     void OnGUI(){
    3.     float myValue = GUI.HorizontalSlider(new Rect(Screen.width-520, _topMargin, 500, 30), myValue, -0.1F, 0.5F);
    4.     GUI.Label(new Rect(Screen.width, 20, 100, 20), "Slider Label");
    5.     }
    6.  
    7.  
     
    Last edited: Feb 26, 2020
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well with that code you can just assign a value to myValue and the slider will reflect it.

    But you shouldn't use that code. OnGUI() is part of the old, obsolete, deprecated, badly-performing "Immediate mode GUI" system, and there is no reason to use that system in game these days.
     
  3. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Yes of course I know the slider value is set by myValue.
    I need to update this slider at a later time dynamically. No way to do this if my slider is created with OnGUI?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The thing with the IMGUI is that the slider object is continually recreated every frame, which is why it performs so badly. There's no such thing as "a later time" when it comes to IMGUI.
     
  5. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Got it. Makes sense.