Bools not working on If statement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class CardMechanics : MonoBehaviour
{
    public StatusBars statusBars;
    GameObject ZombieCard1, ZombieCard2, ZombieCard3, ZombieCard4, ZombieCard5;
    int randomnumberpicker;
    private static bool leftChoice, rightChoice;
    void Start()
    {
        statusBars = FindObjectOfType<StatusBars>();
    }


    //Wouldn't ZombieCards() get called before you have a chance to trigger OnMouseDown(),
    //causing it to instantiate a card (which I presume would create an object with tag "Card") and
    //result in it never entering that initial if statement in ZombieCards() again?
    void Update()
    {
        if(GameObject.FindWithTag("Card") == null)
        {
            ZombieCards();
            leftChoice = false;
            rightChoice = false;
        }
    }

    void ZombieCards()
    {
         randomnumberpicker = Random.Range(0, 6);
         Debug.Log(randomnumberpicker);
        switch (randomnumberpicker) {
         case 0:
            {
               ZombieCard1 = Instantiate(Resources.Load("Frontcard1")) as GameObject;
               ZombieCard1.transform.Translate(0, -1, 0);
                if (leftChoice == true)
                {
                    Debug.Log("Jesus Marty! you fixed it! Great Scott!");
                }
                    break;
            }
            case 1:
            {
                ZombieCard2 = Instantiate(Resources.Load("Frontcard2")) as GameObject;
                ZombieCard2.transform.Translate(0, -1, 0);
                    break;
            }
            case 2:
            {
                ZombieCard3 = Instantiate(Resources.Load("Frontcard3")) as GameObject;
                ZombieCard3.transform.Translate(0, -1, 0);
                    break;
                }
            case 3:
            {
                ZombieCard4 = Instantiate(Resources.Load("Frontcard4")) as GameObject;
                ZombieCard4.transform.Translate(0, -1, 0);
                    break;
                }
            case 4:
            {
                ZombieCard5 = Instantiate(Resources.Load("Frontcard5")) as GameObject;
                ZombieCard5.transform.Translate(0, -1, 0);
                    break;
                }
        }
    }
void OnMouseDown()
    {
        if (gameObject.CompareTag("LeftArrow"))
        {
            leftChoice = true;
            Debug.Log("Left arrow is working");
        }
        if (gameObject.CompareTag("RightArrow"))
        {
            rightChoice = true;
            Debug.Log("Right arrow is working");
        }
    }
}

The problem with this code is that my Left or Right choice bool statements are never initializing the if statement within case 0:
Any help would be appreciated! I think it because of a logic area, the fact that OnMouseDown never has a chance to get triggered. Any help would be much appreciated.
Still new to coding so any pointers would also be very helpful.

A GameObject has only 1 tag, you are checking with “CompareTag” 2 differents tags, that means you probably are doing something really wrong.

What you want (probably) to do, is having 2 gameobjects (1 with left Arrow and 1 with right Arrow)? that when clicked sets the correct booleans in your function right?

You don’t need tags for that:

class LeftArrow: MonoBehaviour
{
    private bool pressed = false;
    public bool IsPressed(){
        if(pressed)
        {
            pressed = false; // trigger the click only once
            return true;
        }
    }
    void OnMouseDown(){
        pressed = true;
    }

    void OnMouseUp(){
        pressed = false;
    }
}

class RightArrow: MonoBehaviour
{
    private bool pressed = false;
    public bool IsPressed(){
        if(pressed)
        {
            pressed = false; // trigger the click only once
            return true;
        }
    }
    void OnMouseDown(){
        pressed = true;
    }

    void OnMouseUp(){
        pressed = false;
    }
}

public class CardMechanics: MonoBehaviour {
    private LeftArrow left;
    private RightArrow right;

//...

    void Awake(){
        LeftArrow = (LeftArrow) FindObjectOfType( typeof( LeftArrow));
        RightArrow= (RightArrow) FindObjectOfType( typeof( RightArrow));
    }

// ...

    void ZombieCards()
    {
         //...
         case 0:
            {
                //...
                if (left.IsPressed())
                {
                    Debug.Log(" FIXED");
                }
           //...
     }

     //...
}

Hahah, with my limited knowledge it was the only idea that worked. It seems to trigger properly too, let me try your fix. I appreciate the help.
Also the arrows are two different objects, so they have different tags each to their own.

Yes, I Imagined the arrows were different objects: Please note that my solution is Ugly, but at least works with what you already know of Unity and C#:slight_smile: .

Glad to be of any help :slight_smile:

P.S Attach each one of the 3 scripts to 3 different GameObjects (LeftArrow on the one tagged “LeftArrow” etc…)

Also note that this way you no longer need “tags”