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

Mathf.Clamp not working correctly

Discussion in 'Scripting' started by j_campbell, Feb 26, 2015.

  1. j_campbell

    j_campbell

    Joined:
    Dec 8, 2014
    Posts:
    33
    Hi I am trying to cap the max limit on a power bar (so that it doesn't drop below 0 and doesn't rise above 500):
    The power bar is revealed by a dynamically controlled mask but this cap isn't working.

    Mathf.Clamp(energy, 0.0F, 500.0F);
    RectTransform rTrans = (RectTransform) transform.GetComponent<RectTransform>();
    rTrans.sizeDelta = new Vector2(200,energy);
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You need to reassign the value
    energy = Mathf.Clamp(energy, 0.0F, 500.0F);
     
  3. j_campbell

    j_campbell

    Joined:
    Dec 8, 2014
    Posts:
    33
    Perfect - thanks