- Home /
RectTransform - How to change Height?
I'm trying to find some way to change the RectTransform height by script without success... I can change the scale, but is not what I want to do.
I hope that exist some method (as bellow) or solution to change this property by script.
RectTransform rectTransform = containerList.transform.GetComponentsInChildren<RectTransform> ();
rectTransform.SetWidthHeight(new Vector2(x,y));
... or any other solution to change this property by script. Even looking the class code I did't found nothing about width and height.
public sealed class RectTransform : Transform
Nothing here too: http://docs.unity3d.com/ScriptReference/RectTransform.html
What I'm missing? If someone could help me... Thanks a lot!
PERFECT!!!! PERFECT!!! PERFECT!!! Thank you VERY much @Addyarb!!!
I have posted my comment as an answer below. Please feel free to mark it as correct.
Answer by Addyarb · Jun 17, 2015 at 11:52 PM
Use sizeDelta.
RectTransform rT;
void Start(){
rT = GetComponent<RectTransform>();
}
void Update(){ //Increase height of UI element.
rT.sizeDelta = new Vector2(rT.sizeDelta.x,rT.sizeDelta.y + 0.5f);
}
Just to increase the answer.. Here is the code in my project:
GameObject go = Instantiate(prefabGameRow,Vector2.zero,Quaternion.identity) as GameObject;
go.transform.SetParent(containerListGames.transform);
go.transform.localPosition = new Vector2(gameRowPosX,totalGames*distanceRowsX);
go.transform.localScale = Vector3.one;
RectTransform rectTransform = containerListGames.transform.GetComponentInChildren<RectTransform> ();
rectTransform.sizeDelta = new Vector2(800,totalGames*distanceRowsX);
Again! Thank you very much @Addyarb!
Answer by Tekksin · Jun 17, 2015 at 11:13 PM
on the object with the height you wanna change:
public float theChange;
transform.localscale.y = theChange;
I already try to change scale, and I said I can but is not what I want to do. Anyway, the way you suggest will not work because "Cannot modify a value type return value of `UnityEngine.Transform.localScale'. Consider storing the value in a temporary variable".
Even using this:
containerListGames.transform.localScale = new Vector2 (800, totalGames * distanceRowsX);
Is not what I want, this works, but will change the scale and not the height, I want to change the height for some reasons in the context of the UI.
Again.. I want to change the RectTransform HEIGHT, not the scale. Thanks for you attention @Tekksin
Please note I wrote "transform.localScale.y" not "transform.localScale". Let me re-read your post.
I think you need to be a little more thorough. Do you want to make it taller from the base upwards? Like scale it up in the way a blade of grass would grow from the ground? Like in the way a bar graph would animate upwards?
I have to admit I thought you were talking about a rectangle gameobject lol. Didn't realize you were using the new UI stuff.