- Home /
Boolean From Another Class Not Being Updated
Hello, I'm trying to implement a rather simple different modes system into my game. The game starts in "Build Mode" where players can move around using camera in top down view. When the player clicks a button they get into the "Character Mode" where they can move their character in thrid person.
So all I need is a simple boolean to keep track of the modes. I have declared the "inCharacterMode" variable in one class. Where we change it as we click on the button. The change works (tested).
The problem is; when I start the scene, the initiated boolean is called from the other class correctly, but somehow I cannot update it at all. The boolean stays the same in the calling class. But it changes in the declared class.
public bool inCharacterMode = false;
public void ChangeViewsButton()
{
if (inCharacterMode)
{
Debug.Log("Character Mode: " + inCharacterMode);
inCharacterMode = !inCharacterMode;
}
else
{
Debug.Log("Character Mode: " + inCharacterMode);
inCharacterMode = !inCharacterMode;
}
}
The calling/referencing class:
void MoveCamera()
{
if (GetComponent<UINewGame>().inCharacterMode == false)
{
.
.
.
.
}
}
inCharacterMode boolean is not being passed on to the second class somehow. I would appreciate any help. Thanks in advance.
GetComponent<UINewGame>().ChangeViewsButton();
? Just so you know - you code, as it is now, is not passing anything.
Thanks for your reply. Do you mean that I should call the method not the variable?
Yes. Also - i find your code confusing. Your english says one thing but your c# says something different. ==
operator checks for boolean equality between LHS and RHS values, and definitely it's not changing anything.
if (GetComponent<UINewGame>().inCharacter$$anonymous$$ode == false)
translated into plain english means: If field "inCharacter$$anonymous$$ode" in my component of type UINewGame has value "false" then ...
Yeah, sorry my messed up explanation. So, there's that button method which changes the boolean from true to false or false to true (It is in UINewGame class). And there's another class, where the $$anonymous$$oveCamera method exists. $$anonymous$$oveCamera should only read "inCharacter$$anonymous$$ode", not change it at all. According to the boolean being false, it should enter that if statement. If it is false, then it should not use the moveCamera method. And btw, since the changeViewsButton method is void I cannot use it for bool comparison. And I do not need it to be boolean type since it should not return anything.
Try something this: https://pastebin.com/ChFfCywh . Putting gameobjects in second argument of Debug.Logs will make that gameobject selected when you click on that log message in console - giving you the ability to figure who is calling who exactly. It's possible there is more than one instance of your class out there.
That was what I thought of too. $$anonymous$$y code might be creating multiple instances. I'll try what you've suggested when i get back home.
Another issue is, the button method only works twice, then it stops outputting debugs. Im getting confused about the user friendliness of Unity. Ive written codes in many languages but still I cannot comprehend the simplest thing in here.
Answer by ATLGAN · Feb 28, 2020 at 02:03 PM
You can make static of 'inCharacterMode' variable. If you do this you can call whenever you want easly:
void MoveCamera()
{
if (UINewGame.inCharacterMode == false)
{
.
.
.
.
}
}
Thanks for your answer! This was the solution. I changed the variable that I wanted to be read to
public static bool
Then created a private static bool in the other class that needed to read the value.
Now the system works, thanks for everyone's time.
Answer by tormentoarmagedoom · Feb 27, 2020 at 03:26 PM
Hello.
Why this "if" and "else" do exactly the same?
if (inCharacterMode)
{
Debug.Log("Character Mode: " + inCharacterMode);
inCharacterMode = !inCharacterMode;
}
else
{
Debug.Log("Character Mode: " + inCharacterMode);
inCharacterMode = !inCharacterMode;
}
This will do exactly the same:
public void ChangeViewsButton() { Debug.Log("Character Mode was: " + inCharacterMode); inCharacterMode = !inCharacterMode; }
And then, about your problem. you say "The boolean stays the same in the calling class. But it changes in the declared class." I dont get what you mean... The inCharacterMode "lives" in the first script. The second script just read the value of that variable, but is never "inside it", just read it from the 1st script...
Do this to see what's the value of the bool when trying to read it from the camera script:
void MoveCamera()
{
Debug.Log ("From Camera is: " + GetComponent<UINewGame>().inCharacterMode);
if (GetComponent<UINewGame>().inCharacterMode == false)
{
If is not what is suposed to bem is because there is anothor script changing its value.
Thanks for your reply. I added that Debug Log line inside the method before posting the question here and what it showed was, always the same state of the boolean. If started as False then it was always False, and vice versa.
What I need is, the "inCharacter$$anonymous$$ode" variable will change very often during gameplay, and when it is True, I want that particular code segment to not work. I'll add a new camera method for when it is True.
Problem is, I can't read the boolean from the other class whenever I want.
Answer by Zentiu · Feb 28, 2020 at 05:31 PM
If a button you click gets you into 'Character mode' then why not make a method for that and let the button call for it and visa versa let anothet button call for the oposite? The method itself wll handle all the logic that you want to make the transition happen.
The camera method is called every frame in another class, I want the buttons to be in a separate class for a more organized setting.