Search code examples
unity-game-enginesliderngui

Building 2DTerrain in Unity3D


I am using NGUI kit in Unity3D to build a progress bar. UISlider script has the value ranging from 0 to 1 to control the slider.

My script is on the same object and i am using this way to set its value according to time (60sec).

this.gameObject.GetComponent().sliderValue = _______ ;

But it is not getting it done. Kindly help me out.


Solution

  • You are not asking for the right type UISlider:

    this.gameObject.GetComponent<UISlider>().sliderValue= _______ ;
    

    Btw it is good practice to assume anything you get returned maybe a null (afterall you may not have the component attached), so it would be better to:

    UISlider lMySlider= this.gameObject.GetComponent<UISlider>();
    if(lMySlider!=null)
    {
        lMySlider..sliderValue= _______ ;
    }
    else
    {
        Debug.Log ( this.gameObject.name " is missing the UISlider")
    }