Search Unity

How to change the size of a UI Image from code? (Trying to make a simple healthbar from this)

Discussion in 'UGUI & TextMesh Pro' started by AAA_Dev, Aug 27, 2014.

  1. AAA_Dev

    AAA_Dev

    Joined:
    May 20, 2014
    Posts:
    11
    Hello, I'm using the new UI system from Unity 4.6 beta...

    Tried different codes, looked at the docs and searched around but can't find the answer...

    I created an Image object inside of a UI Canvas. I'm trying to make a vertical health bar using it, so I need to be able to modify the bar's height at runtime. The new Image object's height/width is adjusted during development time using its RectTransform component, as you see in the screenshot. But how to modify that at runtime?

    I currently have the Image object dragged into a GameObject variable of my main game script and have tried stuff like this (using CSharp):

    public GameObject healthbar;
    healthbar.GetComponent<RectTransform>().rect.Set(0,0,100, 300);

    which doesn't work. And healthbar.GetComponent<RectTransform>().rect.y is GET only so cannot be changed at runtime.

    I've also tried:
    healthbar.transform.localScale.y = 15;

    which doesn't work either.

    what is the proper way to change size at runtime? You can give me an example in either JS or C#, doesn't matter.

    thanks a lot!

     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    My guess is that the rect property of RectTransform is giving you a copy back so Set() is being called on the copy. Try assigning rect to some local variable, Set()ing that, and then re-assigning back to the RectTransform.
     
  3. Anh7

    Anh7

    Joined:
    Apr 4, 2014
    Posts:
    2
    This not work, please help, I stuck with that.
     
    nazifh06 likes this.
  4. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    Find Image component and set requred size:
    Code (csharp):
    1.  
    2. _image.rectTransform.sizeDelta = new Vector2(width, height);
    3.  
     
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    Thanks for this correct reply. Using this way, we can set size of Image through script.

    Code (CSharp):
    1. _image.rectTransform.sizeDelta = new Vector2(width, height);
     
  6. game_factory_

    game_factory_

    Joined:
    Sep 23, 2016
    Posts:
    2
    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    public class whatEver : MonoBehaviour {

    void Update()
    {
    if(Input.GetKeyDown(KeyCode.R))
    {
    transform.localscale = new Vector3(2, 2, 2);
    }

    if(Input.GetKeyUp(KeyCode.R))
    {
    transform.localscale = new Vector3(1, 1, 1);
    }
    }

    }
     
  7. Mr_Mystery

    Mr_Mystery

    Joined:
    Feb 5, 2018
    Posts:
    12
    I think the solution you want is you want to push the image resolution to the rect transforms width and height?
    Code (CSharp):
    1. public RectTransform rt;
    2. public Image image;
    3.  
    4. public void SomeFunction()
    5. {
    6. rt.sizeDelta = new Vector2 (image.sprite.rect.width,image.sprite.rect.height);
    7. }
    Hope I helped anyone!
     
  8. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    No, no, no. NEVER use that method to set the size of a RectTransform, it is compeltely wrong. It will *sometimes*, because of *coincidence* have a *side-effect* of setting the size.

    See here for the actual correct API method for setting size, and detailed explanation (in the thread) of why this is correct: https://forum.unity.com/threads/modify-the-width-and-height-of-recttransform.270993/#post-4053235

    (PS: I hate that someone at Unity chose such a bizarre naming scheme for the API, and deleted/blocked the obvious "size" - but that's what we have to deal with :))
     
    Fingerbob and naviln like this.
  9. wolfomat

    wolfomat

    Joined:
    Oct 19, 2014
    Posts:
    24
    as posted on the other thread, the call to SetSizeWithCurrentAnchors won't work?
    somehow the size of the gameobject is not changing.

    Turned out i've searched for the wrong element. it's now doing something ;)
     
    Last edited: Oct 17, 2020
  10. Aggelas

    Aggelas

    Joined:
    Mar 20, 2021
    Posts:
    1
  11. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    This is completely wrong, you were too lazy to read the thread (which explains IN DETAIL why your post is wrong). Don't post stuff you don't understand on threads you didn't read.

    All you've done is post an advert for a YouTube video - that is itself badly incorrect.