- Home /
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.
}
}
Answer by VesuvianPrime · Feb 08, 2015 at 09:16 PM
You probably want:
health -= 100;
(Which is:- health = health - 100
)
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.
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
![](https://koobas.hobune.stream/wayback/20220613182825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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