- Home /
How to find width and height of game object, Unity2D
Hi everyone!
I have stacked at my code and I'm not sure how to find details (width & height) of my game object. I tried to set component of RectTransform and tried code like this:
float width = rt.rect.width; // Here is problem
float height = rt.rect.height; // Here is problem
GameObject mynewball = (GameObject)Instantiate(ball);
RectTransform rt = (RectTransform)mynewball.transform;
width = 100f;
height = 50f;
My code is not correct so, how can i declare and find my Height and Width of Object through C# ? So later on i can change size (height & width) through code?
Answer by DiegoSLTS · May 04, 2015 at 11:38 PM
I don't get it, that's your code? That shouldn't even compile.
First, you're using "rt" before declaring it. Second, "rect" is a property that you can get, you can't modify the values directly. Third, once you write "float width = rt.rect.width" the "width" variable has a copy of the rect's width, so changing the value of that variable won't change anything.
Your code to get the width should be in the inverse order:
GameObject mynewball = (GameObject)Instantiate(ball);
RectTransform rt = (RectTransform)mynewball.transform;
float width = rt.rect.width;
float height = rt.rect.height;
If you want to change the width and height manually it depends on the anchors. If the UI element is centered inside the parent then rt.sizeDelta is a Vector2 with rt.sizeDelta.x being the width and rt.sizeDelta.y the height, and to change those values you replace the sizeDelta like this:
rt.sizeDelta = new Vector2(100f, 50f);
If the element is not centered you have to change sizeDelta too, but x and y means something different. Look here: http://docs.unity3d.com/460/Documentation/ScriptReference/RectTransform-sizeDelta.html
O$$anonymous$$,so now Unity response to rt. but here is my code, and I have one GameObject who must resize but when I run game, in console i only got 100000000+ clones of that Game Object. From this code I want to change GameObjects's width and height if is bigger than 0.
public class BallResizing : $$anonymous$$onoBehaviour {
public GameObject ball;
float width;
float height;
GameObject myball;
RectTransform rt;
// Use this for initialization
void Start () {
myball = (GameObject)Instantiate(ball);
rt = (RectTransform)myball.transform;
width = rt.rect.width;
height = rt.rect.height;
}
// Update is called once per frame
void Update () {
if (width > 0 && height > 0) {
rt.sizeDelta = new Vector2(500f, 100f);
}
}
BallResize is attached to the "ball" prefab? If so, each time you instantiate a new ball, it's Start method is executed and a new ball is instantiated, and so on. I guess that's why you get thousands of clones.
I'm not sure why you need to resize them to a constant 500x100 value in the Update, can't you just change the prefab to have that size? It looks like you want some method to hide things ("if width and height are 0, they stay at zero. if not, they're stay at 500x100"), but this looks weird, you can just deactivate them or add a CanvasGroup component that lets you change the alpha to 0 to make it transparent.
Answer by josefvesely · Aug 15, 2017 at 08:15 PM
Hello, I would suggest very simple solution:
float width = GetComponent<SpriteRenderer>().bounds.size.x;
Thanks, you answer is simple and correct. I nedded to use correctly raytraces to detect bounds and works perfectly.
Regards.
This gives me world coordinates width. Do you know how to get it in pixels?