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 /
avatar image
0
Question by IsaiahKelly · Oct 01, 2011 at 09:42 PM · floatvectorlocalscaleconverting-var-type-to-other-var-type

Convert localScale to float?

I would like to convert a transform.localScale value into a float. I'm very new to unity and JavaScript and these variable types are driving me insane. I'm use to GameMaker's simplicity because all the variables in that program are basically the same type so you don't have to worry about different types conflicting.

I've also been running into this kind of problem with all kinds of variables because I just don't know how you normally handle converting value types. So if there's a reference somewhere someone could show me that explains how to convert values between common variable types I would greatly appreciate it.

Thanks!

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
5
Best Answer

Answer by aldonaletto · Oct 01, 2011 at 10:04 PM

localScale is a Vector3 - it's composed by 3 floats, x, y and z. This type is used by Unit to hold points, vectors and other x y z things - like scale and rotation angles: you can scale in or rotate around x, y or z axes, so storing these values in Vector3 makes sense.
You could have a single float to represent the equivalent scale - but only if the scale was the same in all three axes (what is a very commom case). If you want to set the scale at once, you can use this:

 transform.localScale = Vector3.one * scale;

This will multiply the vector 1,1,1 by your scale, so all three components will become equal to your scale value (if scale = 0.35, the result will be 0.35, 0.35, 0.35).
The other way around - converting localScale to a float - can be done assuming that the scale is uniform (all three values equal): just read one of them and assign to scale:

 scale = transform.localScale.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 Bunny83 · Oct 02, 2011 at 02:16 AM 0
Share

Well explained ;)

avatar image syclamoth · Oct 02, 2011 at 05:57 AM 2
Share

Or, you can use transform.localScale.magnitude- but be aware that it will give you funny results if the scale is non-uniform.

avatar image IsaiahKelly · Oct 02, 2011 at 05:58 PM 0
Share

Thanks for the detailed response aldonaletto! I'm still trying to get my $$anonymous$$d around all the different variable types and what data they hold.

I totally forgot about how you can access just part of a vector using .x/.y/.z That seems to have solved the problem because I just remembered that not all scale values are the same and that unity would always give me a "can't convert" error every time I tried getting the whole vector as a float, but only getting part of the vector works fine.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Link a value to another value in the Inspector 1 Answer

Vector3 constructor precision 1 Answer

Understanding Arithmetic & Vector Data 1 Answer

X,Y,Z coords of vector greater or equal or lesser or equal comparison 1 Answer

String can't be converted to float or int? 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