Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DayyanSisson · Jul 14, 2012 at 10:38 PM · character controllervaluereturnmodifycentering

Modifying CharacterController center (C#)

I'm getting this error when attempting to change the center of the character controller:

 controller.center.y = controller.height/2;

Cannot modify a value type return value of 'UnityEngine.CharacterController.center'

I've had these problems with trying to edit the rotation and position of objects but I figured out a way around those. This here, though, I am clueless.

How should I do this? Thanks in advance!

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

Answer by Avaista · Jul 14, 2012 at 11:41 PM

Center itself is a property. It can be retrieved and set, but not modified. Most of the time this doesnt matter, as you get a reference type. But with structs like Vector3, its another story

So this is valid

 Vector3 tempCenter = controller.center;
 tempCenter.y = controller.height/2;
 controller.center = tempCenter;

as is this

 controller.center = new Vector3(controller.center.x,controller.height/2,controller.center.z);

but you cannot modify a value of a property that is value type (struct)

You can modify a variable on a reference type return(a class)

 public class Bacon:Yummy
 {
     public int healthRestore;
 }
 public class MyClass
 {
     private Bacon myBacon;
     public Bacon propertyExample
     {
         get{    return myBacon;}
         set{myBacon = value;}
     }
 }

 MyClass example = new MyClass();
 int hp = example.propertyExample.healthRestore;

Is valid ONLY because Bacon is a class, and therefore a reference type. When you access the property, you are NOT modifying the returned value, since the reference to bacon remains unchanged.

I do not know if I explained that well, let me know if you would like clarification and where.

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 DayyanSisson · Jul 14, 2012 at 11:56 PM 0
Share

No that makes sense. I was just wondering though, how does UnityScript bypass it?

avatar image Avaista · Jul 15, 2012 at 12:26 AM 0
Share

I don't use UnityScript, so I cannot say for sure. Could you send me a link to an example where, and I will try to answer to the best of my ability. Remember that it is completely fine to access the value of a variable in a value type property... Example

  Debug.Log(transform.position.y);

...because this does not modify the value.

GET//O$$anonymous$$AY

 return transform.position;

SET//O$$anonymous$$AY

 transform.position = Vector3.zero;

$$anonymous$$ODIFY//NOT SO $$anonymous$$UCH

 transform.position.x += 9001;

GET FIELD//Okay, the property value has not been changed

 float height = transform.position.y;

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

transform.position related compile error fixed don't understand why ? 1 Answer

What part of the delegate gets passed back?The return type? 1 Answer

Sendmessage - return value 1 Answer

How is this always the same outcome?? Caution: contains sex! ;) 1 Answer

Access a class on another script? 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