- Home /
Calling a variable from another script overwrites it?
Am just doing that. I have a bool: [HideInInspector] public bool exampleBool = true; Originally It was only true if it was marked in the inspector, but I changed it to this so it would always be true. Anyway, am calling it from its own script several times just to debug and is always true, as it should. But am also calling it from the other script and is always false. In fact, am not even changing its value. I know that the problem is that the second script overwrites the true value to false becase I created this:
public void testValue() { print("exampleBool: " + exampleBool); }
Now, this is the thing: am calling this void from both its own script and another one, both in Update(); The result from its own is true (as it should), but the other is false.
So this is why am assuming that calling the variable from other script overwrites it, but I don´t know why.
So a few things:
If your bool is always true then it probably doesn't need to exist.
If it needs to be configurable but constant make it a constant.
Bools are structs and have a default value and by default a bool is false if you are accessing it before its explicitly set then it will be false. So either set it to true in its declaration or make sure you are accessing it after it is set.
It´s not always true. That was my way of confir$$anonymous$$g that the other scropt was overwriting it. Its not constant. The deafult value is false, but as I wrote, it´s set to true in its declaration: [HideInInspector] public bool exampleBool = true; Am also setting it to true in Awake() just to be completely sure, then accesing it from Update().
The HideInInspector
attribute does not remove the cached serialisation on fields.
$$anonymous$$akes a variable not show up in the inspector but be serialized.
There is a chance that it is set to false in the inspector which would be overriding you setting it in the initialisation. It shouldnt be overriding the one in your awake though. But if you dont need it to be serialised and want to have a variable that isnt visible in the inspector why not use a property instead?
How are you referencing the GameObject/ method in the other script?
public GameObject[] rigidBodies; $$anonymous$$eshInfo[] info;
void Start()
{
info = new $$anonymous$$eshInfo[rigidBodies.Length];
for (int i = 0; i < rigidBodies.Length; i++)
{
info[i] = rigidBodies[i].GetComponent<$$anonymous$$eshInfo>();
print("bool: " + info[i].exampleBool);//stays false
}
}
Am not using properties because I don´t know them... What are those?
Don't let other script modify the value but still can read the value. public bool exampleBool { get; private set; } void Start(){ exampleBool = true; }
Thanks a lot. It works! But I have a question: If I were to want to modify the value from the second script, what should I do? Because this unables any possible modification. Right now is not my need, but it might be useful in the future.
To modify the value from other script, you can make separate methods or get set accessors
public bool exampleBool{ get; private set; }
public bool ExampleBool{ set{ exampleBool = value; }}
// or
public void SetExampleBool(bool newValue){ exampleBool = newValue; }
// To other scripts
public ScriptThatHasExampleBoolOnIt scriptThatHasExampleBoolOnIt;
void Start(){
scriptThatHasExampleBoolOnIt.ExampleBool = true;
// or
scriptThatHasExampleBoolOnIt.SetExampleBool(true);
}
Answer by ludovicb1239 · Jan 01, 2021 at 09:51 PM
The first script is correct. I would just do in Start() { exampleBool = true;} and in the second script in Update(){ Debug.Log(FirstScript.exampleBool); } Hope it helps a bit
Your help is appreciated, but it doesn´t really work. I´ve been debuging it in Start() and Update() of the second script and is always false. I tried the same with the original script and it shows true always, so for some reason calling it from the other script overwrites it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613022525im_/https://answers.unity.com/themes/thub/images/avi.jpg)