JohnDevince
JohnDevince

Reputation: 21

Bool not returning true in IF statement C# Unity

GameObject ZombieCard1, ZombieCard2, ZombieCard3, ZombieCard4, ZombieCard5;
int randomnumberpicker;
public static bool leftChoice, rightChoice;
void Start()
{

    statusBars = FindObjectOfType<StatusBars>();
}


void Update()
{
    ZombieCards();

}

void ZombieCards()
{
    if (GameObject.FindWithTag("Card") == null)
    { 
     randomnumberpicker = Random.Range(1, 5);
     Debug.Log(randomnumberpicker);
     if(randomnumberpicker == 1)
        {
           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!");
            }
        }
     else if(randomnumberpicker == 2)
        {
            ZombieCard2 = Instantiate(Resources.Load("Frontcard2")) as GameObject;
            ZombieCard2.transform.Translate(0, -1, 0);
        }
     else if (randomnumberpicker == 3)
        {
            ZombieCard3 = Instantiate(Resources.Load("Frontcard3")) as GameObject;
            ZombieCard3.transform.Translate(0, -1, 0);
        }
     else if (randomnumberpicker == 4)
        {
            ZombieCard4 = Instantiate(Resources.Load("Frontcard4")) as GameObject;
            ZombieCard4.transform.Translate(0, -1, 0);
        }
     else if (randomnumberpicker == 5)
        {
            ZombieCard5 = Instantiate(Resources.Load("Frontcard5")) as GameObject;
            ZombieCard5.transform.Translate(0, -1, 0);
        }
    }
}
 void OnMouseDown()
    {
        if (gameObject.CompareTag("LeftArrow"))
        {
            leftChoice = true;
            Debug.Log("Left arrow is working");
            return;
        }
        if (gameObject.CompareTag("RightArrow"))
        {
            rightChoice = true;
            Debug.Log("Right arrow is working");
            return;
        }
    }
}

I posted this earlier but due to lack of clarity (on my part) I can't figure out why this if statement

if (leftChoice == true)
        {
            Debug.Log("Jesus Marty! you fixed it! Great Scott!");
        }

isn't executing, the bool is being set to true on mousedown correct?

Any help would be greatly appreciated

Upvotes: 0

Views: 827

Answers (1)

Aspekt
Aspekt

Reputation: 78

Edit: as some people have pointed out in the comments, it may be (and probably is) the case that Update() is getting called before OnMouseDown(). I'm going to assume nothing else calls these functions, so it may be worth calling ZombieCards() from your OnMouseDown() event instead. That way you can be sure the sequence of events is right. (End edit)

Is there a reason you've set LeftChoice and RightChoice to public instead of private inside the class? This generally means something outside the class could be accessing it. Given you're using Update(), I assume you're inheriting from MonoBehaviour, which means you're attaching this script as a component to another GameObject. This means having private _leftChoice and private _rightChoice might be a better option here.

Another thing which may help - Random.Range(lowInt, hightInt) will return a random int between (and including) lowInt and (highInt - 1), so in your code it will never choose 5. I believe the intention is to start from index 0 and do something like this:

    private const bool NumChoices = 5;
    ...
    int randomNumberPicker = Random.Range(0, NumChoices);
    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;
        ...
    }

Of course, you could also just do Random.Range(1, 6)

Hope this helps!

Upvotes: 1

Related Questions