If with boolean not working...

Hello, i’m trying to learn how to use booleans with if and else if … It’s not working as i intend.

I have a player that can equip a sword or a torchlight, but not both at the same time. Function wells individually, but it’s not working when i try to equip the sword when the torchlight is already there, the sword appears… Maybe it has to do with the order of the “else if” in the list??

Thanks a lot for your input:

Here is my code:

using UnityEngine;

public class AppearDisappear : MonoBehaviour
{
    public GameObject sword;
    public GameObject torchLight;
    bool swordEquipped;
    bool torchLightEquipped;
    void Start()
    {
        sword.SetActive(false);
        torchLight.SetActive(false);
        swordEquipped = false;
        torchLightEquipped = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1) &&!swordEquipped)
        {
            sword.SetActive(true);
            swordEquipped = true;
        }

        else if (Input.GetKeyDown(KeyCode.Alpha1) && swordEquipped)
        {
            sword.SetActive(false);
            swordEquipped = false;
        }

        else if (Input.GetKeyDown(KeyCode.Alpha1) && torchLightEquipped) // this is the first part not working
        {
            sword.SetActive(false);
            swordEquipped = false;
        }

        else if (Input.GetKeyDown(KeyCode.Alpha2) && torchLightEquipped)
        {
            torchLight.SetActive(false);
            torchLightEquipped = false;
        }

        else if (Input.GetKeyDown(KeyCode.Alpha2) && !torchLightEquipped)
        {
            torchLight.SetActive(true);
            torchLightEquipped = true;
        }

        else if (Input.GetKeyDown(KeyCode.Alpha2) && swordEquipped) // this is the second part not working
        {
            torchLight.SetActive(false);
            torchLightEquipped = false;
        }
    }
}

All “if” statements use a condition which equates to a bool. C# isn’t broken so you simply need to debug your logic yourself.

Read your input once, don’t repeat then same thing multiple times and simplify your logic.

Your logic above will only ever execute one of the code blocks. Everything is mutually exclusive.

If you want to be able to do both in a single update then line 31 should be a new “if” not “else if” which is obviously a continuation of the previous checks.

Look at your code, if you press alpha1, it’ll only ever get to the first two blocks as one of them will be true.

1 Like

You have three possible states then:

  • Equipping nothing
  • Equipping sword
  • Equipping torch

Two bools is a bad way to represent these three states. Two bools has 4 possible states:

  • Equipping neither sword nor torch
  • Equipping BOTH sword and torch
  • Equipping sword and not torch
  • Equipping torch and not sword.

This problem will only get worse as you add more and more items to this list.

A much better way to represent your system is with an enum, which can have 3 (or as many as you’d like) states. For example:

enum ItemType {
  None,
  Sword,
  Torch
}

public ItemType CurrentlyEquippedItem;
4 Likes

You don’t need to track the equipped states of your items with an additional bool. The fact that an item’s game object is active will tell you that it’s equipped.

Here’s a slightly easier way to do your thing:

using UnityEngine;
public class AppearDisappear : MonoBehaviour
{
    public GameObject sword;
    public GameObject torchLight;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            sword.SetActive(!sword.activeSelf); // toggle sword on and off
            torchLight.SetActive(false); // disable torch
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            torchLight.SetActive(!torchLight.activeSelf); // toggle torch on and off
            sword.SetActive(false); // disable sword
        }
    }
}

It’ll start to get a little long as you add more items because you’ll need to disable all the items that aren’t selected.

A more advanced method:

public GameObject[] items;

    void OnGUI()
    {
        Event e = Event.current;
        if (e.type == EventType.KeyDown)
        {
            int item = (int)(e.keyCode - KeyCode.Alpha0); // key to item number
            if (item >= 0 && item <= 9)
            {
                items[item].SetActive(!items[item].activeSelf); // toggle the selected item
                // disable all the unselected items:
                foreach (GameObject o in items)
                    if (o!=items[item] && o.activeSelf)
                        o.SetActive(false);
            }
        }
    }

You can use the editor to place all your items in the item array and the OnGUI method will then select/toggle the item that corresponds to the pressed key.

Bools would be good if each of the items was individual, as in having or not having one had no impact on the ability to have others. I’d use an enum in your case, and anytime the enum changes, loop through and set everything inactive except the chosen one. You can have as many other items as you want.