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.

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.

You can make static of ‘inCharacterMode’ variable. If you do this you can call whenever you want easly:

void MoveCamera()
                     {
                         if (UINewGame.inCharacterMode == false)
                         {
                               .
                               .
                               .
                               .
                          }
                     }

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.