Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by UrosDev16 · May 04, 2015 at 11:14 PM · c#recttransformheightresizewidth

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UrosDev16 · May 05, 2015 at 07:33 AM 0
Share

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);
         
     }
 }
avatar image DiegoSLTS · May 05, 2015 at 01:19 PM 0
Share

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.

avatar image
13

Answer by josefvesely · Aug 15, 2017 at 08:15 PM

Hello, I would suggest very simple solution:

 float width = GetComponent<SpriteRenderer>().bounds.size.x;

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Papanash · Feb 08, 2018 at 10:25 PM 2
Share

You really saved my day !

avatar image PacoBarba · May 15, 2020 at 09:08 AM 0
Share

Thanks, you answer is simple and correct. I nedded to use correctly raytraces to detect bounds and works perfectly.

Regards.

avatar image tjbarrott · Nov 19, 2021 at 10:59 PM 0
Share

This gives me world coordinates width. Do you know how to get it in pixels?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RectTransform won't Resize 1 Answer

Unity not dividing correctly C# camera pixel width & height 1 Answer

Having an issue with setting the minimum size for a field size 0 Answers

How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges