Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by FishFern · Apr 05, 2020 at 06:46 AM · scripting beginnersizerecttransformnew user

How do I transform/resize a polygon from within a C# Script Component?

Hi Everyone!

I'm currently trying to set up a script that transforms/resizes a polygon drawn the GUI Canvas from within a C# Script within the same object. The Polygon itself is drawn using CiaccoDavide's [Unity-UI-Polygon Extension][1], which appears to house a regular Rect Transform Component.

Basically, what I'm trying to do is made a script that grows/shrinks a polygon depending on a relationship between two variables. The 'base' size of the Polygon is 248x248, and so the (general) formula I'm working with is:

 width = (breathCount / breathCountMax)*248;
 height = (breathCount /breathCountMax)*248;

I've been hunting around various tutorials and queries about Transform, RectTransform, and all kinds of things, but without much (any) luck at all. In the interests of full disclosure, this is my first attempt at a Unity Project (coming from GameMaker), and so I'm probably a little over my head for now.

SO far my attempt has looked like this (along with some subtle deviations, none of which worked):

 public class breathTransform : MonoBehaviour
 {
 
     // Start is called before the first frame update
 
     GameObject obj;
     public RectTransform rT;
 
 
     void Start()
     {
         obj = GameObject.Find("breath_UI_cur");
         rT = obj.GetComponent<RectTransform>();
     }
 
     // Update is called once per frame
     void Update()
     {
         rT.sizeDelta = new Vector2((rT.sizeDelta.x * ((breathing.breathCount / breathing.breathCountMax) * 248)), (rT.sizeDelta.y * ((breathing.breathCount / breathing.breathCountMax) * 248)));
     }

Any help is greatly appreciated, as this query is boggling my mind! It's something that's relatively simple in GameMaker, but I can't wrap my head around it yet in Unity! [1]: https://github.com/CiaccoDavide/Unity-UI-Polygon

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Apr 05, 2020 at 07:18 AM

@CitronMW, I believe it will be easier setting the local scale.

If the base size is 248. then the scale you need in any dimension will be desired size/248. So set those for x and y, for z you can use 1 because UI elements don't have a z size, per se.

for example,

 float scaleX = desiredXSize/248;
 float scaleY = desiredYSize/248;
 rT.localScale = new Vector3( scaleX, scaleY, 1 );
Comment
Add comment · Show 1 · 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 FishFern · Apr 05, 2020 at 07:26 AM 1
Share

Hi! Thanks for the insight, that makes a lot of sense!

At the moment, I'm getting the below error on runtime:

UnassignedReferenceException: The variable rT of breathTransform has not been assigned. You probably need to assign the rT variable of the breathTransform script in the inspector.

Which I don't quite understand. From what I can see, rT is defined in the constructor, and assigned in the Start() function. Where am I going wrong with my variables?

Thanks so much for your help! Unity has always been super daunting to me, but I'm finally starting to make a little headway, so I really appreciate the help! :)

Edit: Scratch that, it was a capitalisation error in my code, which I have now fixed and everything works perfectly! Thanks for your help @streeetwalker, your solution is much cleaner than what I had, too! :)

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

208 People are following this question.

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

Related Questions

RectTransform.rect.size alternating values 0 Answers

Resize UI Panel to the same size as its parent Canvas through code 0 Answers

Adjust RectTransform width to image that is scaled down 1 Answer

How to get the rendered size of an image on screen? (In absolute pixels that changes based on resolution) 1 Answer

How to make a TextMeshPro Object perfectly fit his RectTransform's height ? 0 Answers


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