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 killfast27 · Feb 08, 2015 at 09:39 PM · javascripterrorhealth-deduction

Expressions in statements must only be executed for their side-effects?

trying to make health script, but it gives me this error "Expressions in statements must only be executed for their side-effects"

 var player : GameObject;
 var health = 1000;
 
 function Start()
 {
 
 }
 
 function Update()
 {
     if(health <= 0)
     {
         Debug.log("you are dead");
         Destroy(player);
     }
     
     if(health >= 1000)
     {
         health = 1000;
     }
 }
 
 function OnCollisionStay2D(coll: Collision2D)
     {
         if(coll.gameObject.tag == "Enemy")
         {
             health - 100; //says the error is on this line, but i don't know why.
         }
     }




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

Answer by VesuvianPrime · Feb 08, 2015 at 09:16 PM

You probably want:

 health -= 100;

(Which is:- health = health - 100)

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 Hoeloe · Feb 08, 2015 at 09:47 PM 1
Share

To add to this, you should read up on the difference between a read and a write statement.

health - 100 is a read statement: it only gives you a value to use, but doesn't alter anything. You often use these in mathematical expressions (this particular one will give you a value 100 less than the current value of "health"). The reason the error occurs is that you aren't using this statement - you're asking: "please give me "health", $$anonymous$$us 100", and then not using that value anywhere.

By contrast, health -= 100 is a write statement. It is shorthand for health = health - 100. The difference here is that you have an assignment operator (=). This means "I want to change the value that is stored here". It's worth noting that if you use the full version of this statement (`health = health - 100`), the read expression mentioned above is used as part of the complete expression - we are saying "I want to change the value of "health", and I want it to equal "health" $$anonymous$$us 100".

Unlike before, though, the read expression is not isolated, and is used in an executable expression (all write expressions are executable, but not all executable expressions are write expressions - this might be worth looking up too). Because the expression is executable (it causes some action to be taken in the program), this is valid, and will not result in an error. The read expression is not executable, and so the compiler doesn't know what to do with it, and throws an error.

avatar image killfast27 · Feb 09, 2015 at 05:33 AM 0
Share

Thank you very much ^^ was very informative and it worked. I will remember this and definitely look into it more.

Again 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I have been watching his tutorial but nothing happens 0 Answers

For statement errors? 1 Answer

BCE0049 error with network script 0 Answers

Type could not be resolved because of a cycle. 1 Answer

Similar scripts but error on one and not the other 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