- Home /
Why can't I set a var outside a method?
Why does this come back false? Can't I set that true there? Sometimes this works, sometimes it doesn't.
public class Script : MonoBehaviour
{
public bool example = true;
public void CheckBool()
{
Debug.Log(example);
//Returns false
}
}
I'm sorry if the title is incorrect. I don't know csharp vocab.
Answer by ShadyProductions · Sep 02, 2020 at 09:23 AM
Actually this is correct, but it could be that you overwrote the value in the inspector and then the value persists. Best to make this value private, or use [HideInInspector] if you don't want the inspector value to overwrite whats in your code.
Mainly, this is used to give the inspector a "default" value, but once overwritten it also overwrites in the code.
I was thinking the inspector was to blame. But [HideInInspector] won't work. Still comes back false.
Yes that might be because it's already saved in the .meta file of the corresponding script. $$anonymous$$ight need to adjust it there first :)
I don't see anything in meta. Is there another way to keep it public for other scripts without having the inspector effect it.
Your answer
Follow this Question
Related Questions
Instantiate creating object far off the screen 0 Answers
Drawing a 2D rectangle from point A to point B 0 Answers
Traffic Jam Puzzle-like Game Help 1 Answer
Generate heightmap from Terrain object with code 1 Answer
Change the origin point position 2 Answers