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 Firedan1176 · Dec 19, 2015 at 12:10 AM · setters

Weird setter functionality?

This is essentially what I have:

 private float health;
 
 public float Health {
     set {
             health += value;
     }
 }

. . .

 health -= 100;

For some reason, instead of getting the health and subtracting 100, it will add 100.

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

Answer by Dave-Carlile · Dec 19, 2015 at 02:18 AM

The last line isn't using the setter at all, it's just subtracting 100 from health. To use the setter you would need to use Health -= 100. I think something is missing in the code you posted. In fact, trying to use the property in that way generates the error "The property cannot be used in this context because it lacks a get accessor". Adding the get so it compiles results in -100 as expected. Now, if health starts out as a value other than 0 then you will have some odd behavior as outlined next.

Don't do that sort of thing in a setter. The expected behavior is to set something to the value of an expression. Your code will lead to some very, very confusing behavior, for example...

 Health = 0;
 Health += 10;
 Health += 10;
 Health += 10;
 Health += 10;

Using your setter, what is the value of Health when that is done? Any sane person would say 40. However it's not, it's 150. Why?

First, remember that Health += 10 is the same as Health = Health + 10. And your setter is health = health + value. Combining those, each line is health = health + (health + 10).

 Health = 0;    // health is 0
 Health = Health + 10;   // health = 0 + (0 + 10), health = 10
 Health = Health + 10;    // health = 10 + (10 + 10), health = 30
 Health = Health + 10;   // health = 30 + (30 + 10), health = 70
 Health = Health + 10;   // health = 70 + (70 + 10), health = 150

That is completely unexpected and non-intuitive behavior. Don't do it. The setter should be health = value. If you want something to increment then make a function...

 void AddHealth(float value)
 {
     health += value;
 }

That is intuitive.

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 Firedan1176 · Dec 22, 2015 at 02:25 AM 0
Share

Wow, thanks.. I'm not thinking straight sometimes.

By the way, the health+= was a typo, I meant Health (capital H). Thank you!

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

30 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

Related Questions

Getting a variable works in a Start function, but not in another one. 1 Answer

Dictionary set value with dynamic keys 1 Answer

Getter working, Setter not 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