- Home /
int variable loses its value
Hi, I'm having a weird issue, I have an int variable named "aux", declared in my class Class1. Also I have a second class named Class2 that inherits Class1. In Class 2 I have a method to catch the mouse down event and increment the value of aux.
Aux is declared with a value of 0, so in the first click its value will be incresed to be 1, but in the second click aux value is 0 again. I don't understand why the updated value is lost.
This is my code:
public class Class1: MonoBehaviour{
public int aux= 0;
}
public class Class2: Class1
{
void OnMouseDown ()
{
aux++;
}
}
Can anyone answer this, please? Thanks in advance
With this info we can only guess where it is being set back to 0. You could debug by making aux
temporarily a property ins$$anonymous$$d of a variable and putting a Debug.Log(aux) in the setter. Then you can see where it's being called with a value of 0.
Your answer
![](https://koobas.hobune.stream/wayback/20220613190140im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to keep variable value 3 Answers
How to reset a variable? 1 Answer
'Cannot convert int to String' 2 Answers
Divisible by 3 2 Answers