[C#] Variable init but delete after that
Hello, I am posting here cause I have a problem with some variables and I don't understand why. I am sure it's a basic question asked a million times but I didn't succeed to find any solution to my problem. This is an example of class:
class A : Mono
{
bool b = false;
float f;
void Init ();
{
b = true;
}
public void setV(float _f)
{
if (b)
{
f = _f;
}
}
}
This class is a Slider Manager, I am initializing it at the begining of my game. The Init is called by my script Main. During the game, the user can use a slider to modify the content of this float. But only if the boolean is "true", I have checked with breakpoints, "b" is well init but in the process it looses its value and back to "false". So when the slider calls the setter setV, it always fails. One explaintion could be that the class is deleted between the init and the using, but how I could avoid that? Because it's not done by myself. Thanks a lot for your help, Clément
$$anonymous$$ake b
a property and put a Debug.Log in the setter to see if it changes and when. You can also print out the instance id of the object when changing b or f so you know if the init is initing the same object you are calling setV on.
As such i can't see anything wrong with the logic here. There are some obvious syntax errors though, that would prevent you from running this exact code and that's why I wouldn't analyse this sample too thoroughly because there migh be some other mistake in your actual script.
GetInstanceID was really usefull. I have succeed to find that the class B was created twice, one in my $$anonymous$$ain class and one by Unity. I have replaced "AddComponent" by "GetComponent" to get the one created automaticly. I check with a boolean that my empty object don't pass in the update until I initialize it in my $$anonymous$$ain class. Used to work on C++, I am little bit confused by the working of Unity and its constraincts. Thanks for your help.
What do you mean with "the class is deleted between the init and the using"? Is the gameObject that have the script destroyed or something like that?
if so you would have to run Init() again when it's recreated.