• 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 tinchek · Oct 08, 2015 at 03:31 PM · transform.position

Cannot modify the return value of 'Transform.position' because it's not a variable

I am trying to script so when the object reaches certain height, his height is equal to that height, so he won't go higher.

I have been using this but it says "Cannot modify the return value of 'Transform.position' because it's not a variable"

if (transform.position.y > highestPosition) transform.position.y = highestPosition;

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

3 Replies

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

Answer by Dave-Carlile · Oct 08, 2015 at 04:06 PM

The reasons you can't do this are somewhat complicated. It has to do with how C# deals with struct properties. Transform.position is a property and when you access it it's returning a copy of the position since it's a struct. So modifying any of the fields in the struct ( y in your case) wouldn't have any effect since you'd be modifying a copy. Rather than silently not do anything the compiler gives you an error.

The proper way to handle this is to set the position property to a new Vector3...

 Vector3 p = transform.position;
 if (p.y > highestPosition)
 {
     p.y = highestPosition;
     transform.position = p;  // you can set the position as a whole, just not individual fields
 }

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 Danielhall78 · Jan 29, 2021 at 07:44 PM 1
Share

I tried this however

alt text

now I'm getting this alt text

I'm brand new to coding (1 month) and I've followed along with the tutorial and now I'm trying to free lance a little. Not sure what is going on.

codequestion.png (41.2 kB)
question.png (10.9 kB)
avatar image
1

Answer by paulo_schiavetti · Mar 15, 2020 at 03:08 AM

I think I solved it... this line changes the y position without changing x and z.

 transform.position = new Vector3(transform.position.x, [insert y here] , transform.position.z);
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 Bunny83 · Mar 15, 2020 at 03:49 AM 0
Share

This implementation is worse than what Dave-Carlile posted. "position" is a property which directly calls a method on the native C++ side to read the position of the transform. In your approach you would call that function 3 times to get the same value 3 times. It's generally easier to understand and better for performance to just use a temp variable.

avatar image paulo_schiavetti Bunny83 · Mar 15, 2020 at 04:04 AM 0
Share

For some reason i didn't read Dave-Carlile's answer and it is in fact better, sorry for that...

avatar image
1

Answer by Whiteleaf · Oct 08, 2015 at 04:00 PM

Unity is telling you it can't directly modify the 'transform.position.y' value; I don't know why Unity doesn't let you do this, but this is how you fix it:

 private float y;
 
 void Start()
 {
 y = transform.position.y;
 }
 
 void Update()
 {
 if(y > highestPosition)
 y = highestPosition;
 }


Usually it tells you to store it in a variable, which I did here. If this didn't help feel free to ask more questions!

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 Dave-Carlile · Oct 08, 2015 at 04:06 PM 0
Share

This is close, but you're never actually updating the transform.position again.

avatar image Whiteleaf · Oct 08, 2015 at 05:16 PM 0
Share

Ah, yes you're right. I guess you should put "y = transform.position.y" in the update function, otherwise it'll only be stored as the y position once. Thanks for correcting me!

avatar image Dave-Carlile Whiteleaf · Oct 08, 2015 at 05:40 PM 0
Share

I think you're still missing the point :) transform.position must be set after the y value is capped.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

How to align & rotate objects to each other at run time? 1 Answer

Unity Object Aligment in Different Resolutions and Scales 0 Answers

Change animation at runtime with Mecanim 0 Answers

Transform player from one position to another after completing a goal 1 Answer

transform.position return the wrong value 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