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 SimonaDobre11 · Jun 03, 2017 at 11:31 AM · transformlocalscale

transform.localScale.x - can't change it

Hello, I'm trying to change the direction of a character through this: transform.localScale.x *= -1; The error that I get is : "cannot modify the return value of transform.localScale because it's not a variable"

it works with this code: Vector3 myScale = transform.localScale; myScale.x *= -1; transform.localScale = myScale;

From my point of beginner's view, these 2 code sequences make the same thing. Please kindly tell me why I can't change the code, using the first version. Thank you !

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

Answer by HenryStrattonFW · Jun 03, 2017 at 06:12 PM

The reason you cannot do this is because transform.localScale is a property, not a direct link to the variable itself, so its sort of like the same thing as calling a function like "GetLocalScale()" where the return value is the localScale. Fortunately properties can have get and set parts to them, however when used with structs, you have to set the whole value, you cannot just set a part of it. So the simplest way I've found to do what you're trying here is just to use a temporary Vector3 like this:

 Vector3 lTemp = transform.localScale;
 lTemp.x *= -1;
 transform.localScale = lTemp;

Hope this helps.

Comment
Add comment · Show 4 · 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 Maximetinu · Jun 04, 2017 at 12:01 AM 1
Share

I prefer transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z)

avatar image Bunny83 Maximetinu · Jun 04, 2017 at 01:30 AM 0
Share

I think Henry's snippet is more readable. Such a long line and somewhere in that line is a tiny - that can easily be overlooked.

A shorter one-liner would be:

 transform.localScale = Vector3.Scale(transform.localScale, new Vector3(-1,1,1));
avatar image Maximetinu Bunny83 · Jun 04, 2017 at 01:36 AM 0
Share

You're right, It's my bad habit to code the shortest than possible.

Although in your solution you are guessing that the transform.localScale.y and .z are 1

Show more comments

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

8 People are following this question.

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

Related Questions

clamp limit variables trouble 1 Answer

Crouch Script/ Scaling transform from one side only 1 Answer

Prefab color is black 1 Answer

Can't get Object size to Reset to Original 1 Answer

How is localScale adjusted when re-parenting transforms? 2 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