- Home /
if statement not being accessed - why?
I am tearing my hair out here. The following code doesn't work:
// CAMERA BUTTON
if (CameraButton.HitTest(touch.position) && touch.phase == TouchPhase.Began && LevelSetup.bGameIsInPlay)
{
//Debug.Log("Hi");
if (Cameras.iCamera == 0)
{
Cameras.iCamera = 1; // Set to chase cam
}
else if (Cameras.iCamera == 1)
{
Cameras.iCamera = 0; // Set to cockpit cam
}
else
{
Debug.Log(Cameras.iCamera);
}
}
Even the else statement part doesn't run, but if I get rid of the if statements and just have the Debug.log(iCamera) line, it prints fine.
I know there are no issues with the GUITextures as I have tested and tested and also the static var that I am using (iCamera) works fine as it is being changed via other scripts and as I said it prints out properly when I get rid of the if statements... what the hell can cause this? Driving me nuts!
Is this some sort of bug in Unity? I can't see how it could be and nothing in the debug log suggests anything odd....
Put a debug log statement in each clause. One clause of a if-else statement will run. Also put a debug log statement immediately before the istatement to make sure you are calling your method properly.
I did that... nothing.... if I run that "Hi" line it prints fine.
EDIT: Actually, I just tried that... maybe I hadn't before... but it's printing out all of them... odd...
If iCamera is not changing, and you are getting no output, then one of the three clauses in this line is evaluating to false:
if (CameraButton.HitTest(touch.position) && touch.phase == TouchPhase.Began && LevelSetup.bGameIsInPlay)
Put a Debug.Log() to output the result of each of the three clauses.
Does it enter if (CameraButton.HitTest(touch.position) && touch.phase == TouchPhase.Began && LevelSetup.bGameIsInPlay)
You coudl have an order of operatiosn issue here:
CameraButton.HitTest(touch.position) && touch.phase == TouchPhase.Began && LevelSetup.bGameIsInPlay)
Depending on which takes precedence, && or ==, this coudl have two very different meanings.
ALWAYS FULLY PARENTHESIZE, never ever take order of opwrations for granted. I woudl guess what you mean is
CameraButton.HitTest(touch.position) && (touch.phase == TouchPhase.Began) && LevelSetup.bGameIsInPlay)
Answer by POLYGAMe · Jun 21, 2014 at 01:03 AM
Turns out it wasn't that it wasn't triggering... it was doing it four times!
The issue was that I had the controls inside OnGUI and because I had four cameras, it was running it four times.
I put the code into its own function and ran it from Update and it works fine now.
Thanks for the debugging help, everyone!
Your answer
Follow this Question
Related Questions
GUITexture in Unity 5 0 Answers
GUI.Button is always true? 1 Answer
Cant get touch button to work 1 Answer
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
Texture height based on it width. 1 Answer