Why does my boolean change?
Hello guys
I am doing kinda puzzle game but am struggling with something. My boolean variable crossButtonClicked is behaving strange. I am initializing it false in the beginning and making it true whenever the pointer is over a Cross Button by making it true in the OnPointerEnter event.I made some tests both of these conditions if(crossButtonClicked==true) and else if(crossButtonClicked==false) are returning true, which is strange since i am not changing my boolean anywhere else except in the OnPointerEnter event.
Thanks in advance
public class EventHandler : MonoBehaviour,IPointerEnterHandler
{
private bool buttonClicked=false;
private bool crossButtonClicked = false;
private GameObject youWonPanel;
private Image image;
private GameObject youLostPanel;
void Awake()
{
youWonPanel = GameObject.Find("You Won");
youLostPanel = GameObject.Find("YouLostPanel");
}
void Start()
{
image = gameObject.GetComponent<Image>();
youWonPanel.SetActive(false);
youLostPanel.SetActive(false);
}
void Update()
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch touch = Input.GetTouch(i);
if (touch.phase==TouchPhase.Moved)
{
if (buttonClicked == true)
{
image.color= Color.blue;
}
}
if (touch.phase == TouchPhase.Ended)
{
Debug.Log("touch phase ended " + touch.position);
//Debug.Log((Vector2)GameManager.starButtons[0].transform.position);
//Debug.Log((Vector2)GameManager.starButtons[1].transform.position);
float distance1 = Vector2.Distance(GameManager.starButtons[0].transform.position, touch.position);
float distance2 = Vector2.Distance(GameManager.starButtons[1].transform.position, touch.position);
Debug.Log("DIstance1 is " + distance1);
Debug.Log("Distance2 is " + distance2);
if (distance1 < 50f || distance2 < 50f)
{
if (crossButtonClicked == true)
{
youLostPanel.SetActive(true);
for (int j = 0; j < GameManager.temporaryCross.Length; j++)
{
if (GameManager.temporaryCross[j] != null)
{
GameManager.temporaryCross[j].SetActive(true);
}
}
}
else if(crossButtonClicked==false)
{
youWonPanel.SetActive(true);
Debug.Log(crossButtonClicked);
}
}
else
{
image.color = Color.white;
buttonClicked = false;
}
}
}
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Im on button " + gameObject.name);
if (eventData.pointerCurrentRaycast.gameObject != null)
{
for(int i = 0; i < GameManager.temporaryButtons.Length; i++)
{
if (eventData.pointerCurrentRaycast.gameObject.name == "CrossButtons")
{
Debug.Log("Pointer over crossButton");
crossButtonClicked = true;
}
}
Debug.Log("Mouse Over: " + eventData.pointerCurrentRaycast.gameObject.name);
}
buttonClicked = true;
}
Answer by tormentoarmagedoom · Jun 12, 2018 at 07:38 AM
Good day.
Check your code.
Is not possible that in the same iteracy, it enters inside an "if" and a "else if".
So, Or you have 2 scripts doing things, or the first "if" changes something that makes the code enters the "else "if" at the NEXT ITERACY.
Bye
Sorry for the late reply.I figured it out some days ago.This was the problem, I had another script that made the condition in the else if to become true.Thanks a lot.
Your answer
Follow this Question
Related Questions
Enabling & Disabling Script via On Click 1 Answer
How to inherit a boolean 2 Answers
Bool[] Array index is out of range 0 Answers