Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not able to change bool with UI Button

Discussion in 'Scripting' started by ConArtist99, Jun 19, 2019.

  1. ConArtist99

    ConArtist99

    Joined:
    Nov 8, 2018
    Posts:
    4
    I'm trying to change a couple bool values when I click a UI Button. When I check these bools in the update function or call it from a different script, the bools are false. There are no errors when I run it in the editor. I know that the button is calling the function correctly because when I print the values inside the SideBar function, I get the values I expect. I'm probably missing something obvious. Thanks.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonController : MonoBehaviour
    6. {
    7.     private bool sideBarIn;
    8.     private bool wMLerp;
    9.  
    10.     void Start()
    11.     {
    12.         sideBarIn = false;
    13.         wMLerp = false;
    14.     }
    15.     public void SideBar()
    16.     {
    17.         sideBarIn = !sideBarIn;
    18.         wMLerp = true;
    19.     }
    20.     private void Update()
    21.     {
    22.         print(wMLerp);
    23.         print(sideBarIn);
    24.     }
    25. }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    You're calling SideBar via the onClick event of some button? Can you show a screenshot of the event on your button?
     
  3. ConArtist99

    ConArtist99

    Joined:
    Nov 8, 2018
    Posts:
    4
    Button.PNG
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Looks fine. Are you SideBar() is being called? Put a Debug.Log statement in that method to ensure it's called when you click the button.

    Do you have an EventSystem somewhere in your scene? You'll need one for UI stuff like this to fire.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    My silver-bullet guess is that you have more than one copy of the ButtonController class in your scene, so you are changing the variables on one copy but then looking at the output from Update() on another copy.

    If you say Debug.Log("Some message", gameObject) then when you click that output message in your console it will also select the game object in your hierarchy, allowing you to see exactly which object that message is actually coming from.

    If that's not it, then it could also be that you are changing the values successfully but something else is changing them back. If you right-click a variable in Visual Studio and select "Find All References" it will give you a list of every place in your program that accesses that variable, which could help you identify places it could be changed.
     
    berkekonargocer and ConArtist99 like this.
  6. ConArtist99

    ConArtist99

    Joined:
    Nov 8, 2018
    Posts:
    4
    This was exactly it, for some reason the button was calling the class from a prefab in my assets, not the one in the scene. I dragged the object with the ButtonController script onto the OnClick() field of the button. It works now, thanks.