Boolean seemingly changing out of thin air C#
My code was running perfectly, but all of a sudden things started to go awry. For some reason, one of my booleans will just switch from false to true and vice versa, even though there is no code that would make it do such a thing. You can see in the code below that I have a print statement at the top and at the bottom, and although they should both print out the same boolean value, they do not. The update function is the only function running in this script, so I don't know why this is happening. Any suggestions as to what I should do will be greatly appreciated.
void Update () {
print ("6: " + seaweed1Started);
if (true) {
seaweed1Started = objectPlacement (seaweed1, 200, seaweed1Started, 0);
}
print ("5: " + seaweed1Started);
}
Answer by pekalicious · Nov 15, 2015 at 11:56 PM
"even though there is no code that would make it do such a thing."
You do have code that would make it do such a thing:
seaweed1Started = objectPlacement (seaweed1, 200, seaweed1Started, 0);
You are calling the objectPlacement
method and setting your boolean variable seaweed1Started
to whatever it returns. What has changed in objectPlacement
?
Also, there's no need to write if (true)
because that statement will always be true. Is that temporary code, or are you trying to express something in code that you are not sure how?
The objectPlacement method does change the seaweed1Started boolean, but not between the print statements that are executed. Once the print statement "5" prints, the update function automatically loops back around and prints the "6" statement, not ever running the objectPlacement method.
In regard to the if(true), I was testing out whether methods in the if statement were executed in order or all at once, and I forgot to get rid of it.
Ah, silly me, good point. $$anonymous$$issed that. So where is seaweed1Started co$$anonymous$$g from? Is it a static variable? Who else has access to it and changes it?
Seaweed1Started is not static and it's initialized at the top of the script. Nothing else has access to it and nothing else can change it, other than whatever is causing me this grief.
Answer by CraniumCode · Nov 16, 2015 at 01:56 AM
@pekalicious The script is only going to be used in one place on one gameObject
Hmmm.. It's weird that making it static worked. I'm sure there's more to this, but since there is only gonna be one, then you should be fine for now.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045215im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing Variables on one script with multiple other scripts 1 Answer
How do I Effect a whole Array of GameObjects' Scripted Boolean? 0 Answers
PLEAASEE I NEED HELP!!! How to turn Mesh Renderer of cubes on and off by pushing buttons? 1 Answer
How can i pass a method like a parameter Unity3d C#? 3 Answers
animator boolean 2 Answers