Switching buttons from true to false with enum?

I want to make it so clicking the “Move” button in my UI sets “MoveEnabled” to true and all other bools to false. And I want to do the same for all the other buttons. Is there a way to do this without writing a painfully long If/Else statement?

I’ve looked into enums and switches and they seem close to what I want but I feel like there is a better solution that is easier to expand when adding more buttons/bools in the future.

Here is what I have so far using a ‘for’ statement. I thought this would work but it’s not comparing the NAME of the bools but rather the boolean value. So it’s wrong. How do I compare the names?

using UnityEngine;
using System.Collections.Generic;

public class ActionSwitch : MonoBehaviour
{
    public bool Neutral;
    public static bool toggleMove;
    public static bool toggleLook;
    public static bool toggleProfile;

    public List<bool> activeBool;
    public List<bool> actionSwitch;
 
    private void Start()
    {
        toggleMove = false;
        toggleLook = false;
        toggleProfile = false;

        activeBool.Add(Neutral);

        actionSwitch.Add(toggleMove);
        actionSwitch.Add(toggleLook);
        actionSwitch.Add(toggleProfile);
    }

    private void Update()
    {
    }

    public void menuBools()
    {
        for (int i = 0; i < actionSwitch.Count; i++)
            if (actionSwitch[i] == activeBool[0])
            {
                actionSwitch[i] = true;
            }
            else
            {
                actionSwitch[i] = false;
            }
    }

    public void Move()
    {
        activeBool.RemoveAt(0);
        Debug.Log("Movement Enabled");
        activeBool.Insert(0, toggleMove);
        menuBools();
    }

    public void Look()
    {
        activeBool.RemoveAt(0);
        Debug.Log("Look Enabled");
        activeBool.Insert(0, toggleLook);
        menuBools();
    }

    public void Profile()
    {
        activeBool.RemoveAt(0);
        activeBool.Insert(0, toggleProfile);
        menuBools();
    }
}

This code below works… If anyone can suggest something better I would welcome it. This good for now but once I add more buttons it will be cumbersome to remember what Int corresponds to what bool.

using UnityEngine;
using System.Collections.Generic;

public class ActionSwitch : MonoBehaviour
{
    public static bool toggleMove;
    public static bool toggleLook;
    public static bool toggleProfile;

    public int activeBool;
    public List<bool> actionSwitch;
   
    private void Start()
    {
        toggleMove = false;
        toggleLook = false;
        toggleProfile = false;

        actionSwitch.Add(toggleMove);
        actionSwitch.Add(toggleLook);
        actionSwitch.Add(toggleProfile);
    }

    private void Update()
    {
    }

    public void menuBools()
    {
        for (int i = 0; i < actionSwitch.Count; i++)
        {
            actionSwitch[i] = false;
            actionSwitch[activeBool] = true;
        } 
    }


    public void Move()
    {
        activeBool = 0;
        Debug.Log("Movement Enabled");
        menuBools();
    }

    public void Look()
    {
        activeBool = 1;
        Debug.Log("Look Enabled");
        menuBools();
    }

    public void Profile()
    {
        activeBool = 2;
        menuBools();
    }
}

I would create a component to represent each action, then add them to the GameObjects with the Button components.
They could each inherit from the same base class so that both adding new actions and managing the active action become simple.

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
public abstract class UIAction : MonoBehaviour
{
    private void Start()
    {
        var actionState = GetComponentInParent<ActionState>();
        GetComponent<Button>().onClick.AddListener(()=>actionState.SetActiveAction(this));
    }
}

To create new actions you would just need to create a new class that inherits from this base class and add it to a new button GameObject.

public sealed class MoveAction : UIAction { }

public sealed class LookAction : UIAction { }

public sealed class ProfileAction : UIAction { }

Then your ActionSwitch class could be simplified to something like this:

using UnityEngine;

public class ActionState : MonoBehaviour
{
    public UIAction activeAction;

    public void SetActiveAction(UIAction action)
    {
        activeAction = action;
    }
}

Wow! This is VERY thorough… I thank you greatly for this.

1 Like