- Home /
Bool works everywhere but specific 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;
public bool leftChoice, rightChoice;
float distance = 10;
void Start()
{
statusBars = FindObjectOfType<StatusBars>();
leftChoice = false;
rightChoice = false;
}
//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.FindGameObjectWithTag("Card") == null)
{
SpawnZombieCard();
}
if (leftChoice == true) Debug.Log("Is true");
}
void SpawnZombieCard()
{
randomnumberpicker = Random.Range(0, 6);
switch (randomnumberpicker)
{
case 0:
{
ZombieCard1 = Instantiate(Resources.Load("Frontcard1")) as GameObject;
ZombieCard1.transform.Translate(0, -1, 0);
ZombieCardReactions();
break;
}
case 1:
{
ZombieCard2 = Instantiate(Resources.Load("Frontcard2")) as GameObject;
ZombieCard2.transform.Translate(0, -1, 0);
ZombieCardReactions();
break;
}
case 2:
{
ZombieCard3 = Instantiate(Resources.Load("Frontcard3")) as GameObject;
ZombieCard3.transform.Translate(0, -1, 0);
ZombieCardReactions();
break;
}
case 3:
{
ZombieCard4 = Instantiate(Resources.Load("Frontcard4")) as GameObject;
ZombieCard4.transform.Translate(0, -1, 0);
ZombieCardReactions();
break;
}
case 4:
{
ZombieCard5 = Instantiate(Resources.Load("Frontcard5")) as GameObject;
ZombieCard5.transform.Translate(0, -1, 0);
ZombieCardReactions();
break;
}
}
}
void OnMouseDrag()
{
if (gameObject.CompareTag("Card"))
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition;
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "True")
{
leftChoice = true;
Debug.Log("Enter is working.");
}
}
void ZombieCardReactions()
{
if (ZombieCard1)
{
Debug.Log(leftChoice);
if(leftChoice == true)
{
Debug.Log("You got it working");
}
}
}
}
**The bool leftChoice, which is changed to true upon the trigger enter doesnt work in the if (leftChoice == true) in ZombieCardReactions();
It actually doesnt effect the if statement at all, but in update I have a Debug.Log that says the bool is actually changing to true. So what gives? Any help would be much appreciated.
Asked the question but could not get any advice that would actually help me.**
Could you check
void ZombieCardReactions()
{
Debug.Log("ZombieCardReactions() is called");
if (ZombieCard1)
{
Debug.Log(leftChoice);
if(leftChoice == true)
{
Debug.Log("You got it working");
}
}
else
{
Debug.Log("The var ZombieCard1 does not evaluate to TRUE");
}
}
?
If leftChoice really is true and ZombieCardReactions() really is called, the only reasonable assumption is that the first if-Statement does not evaluate to true.
Answer by Koen-Matthijs · Jan 30, 2017 at 09:43 PM
Hi
Evaluating your code, below you'll find a scenario how this would happen:
Method ZombieCardReactions has an additional outer IF statement that checks if variable ZombieCard1 actually points to a GameObject instance. Given the randomness you apply in method SpawnZombieCard() it is fairly possible that variable ZombieCard1 is actually null, in which case method ZombieCardReactions() will not reach the second IF statement at all.
It's not a matter of whether or not the bool is correctly evaluated... the code just doesn't get to that point.
With kind regards,
Koen Matthijs