- Home /
How do I add two variables together?
I want to know how to add two variables together but unity keeps saying " Expressions in statements must only be executed for their side-effects."
I don't know how to make the code work correctly
does anyone know what i need to do.
here's the code:
var PointsGiven : float;
function OnTriggerEnter(other : Collider){
if (other.tag == "Robot"){
other.P1Points.Control + PointsGiven;
}
}
Any help would be greatly appreciated.
Answer by KiraSensei · Dec 18, 2013 at 02:55 PM
What is P1Points ? You should cast your collider from "Collider" to the object you expect.
And I think you want to do this :
var PointsGiven : float;
function OnTriggerEnter(other : Collider){
if (other.tag == "Robot"){
other.P1Points.Control += PointsGiven;
}
}
or :
var PointsGiven : float;
function OnTriggerEnter(other : Collider){
if (other.tag == "Robot"){
other.P1Points.Control = other.P1Points.Control + PointsGiven;
}
}
The error has disappeared but when i test it out P1Points doesn't change.
So this is another problem now. You should validate the answer and then open a new one with specific information about it (like post the code about your object that contains P1Points).
It's not because you still have errors that you are not solving your problems :). So don't give up :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613124426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing a Variable in a script from another Object 1 Answer
Build problem, missing variables 1 Answer
Script adding error 3 Answers
Problem with var in C# (Vector3, transform and similiar) 1 Answer