- Home /
The question is answered, right answer was accepted
İf-Else block doesn't react the condition
public class TouchClass : MonoBehaviour {
SliderScr slider;
void Start(){
slider = GameObject.Find("SlideMenu").GetComponent<SliderScr>();
}
void Update(){
for(int i = 0; i < Input.touchCount; i++)
{
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
if(!slider.setUp)
{
//Not working...
}
else if(slider.setUp)
{
//Not working...
}
void OnGUI()
{
GUI.Label(new Rect(50,50,50,50),slider.setUp.ToString());
}
}
I've got a SliderScr script attached to a "SlideMenu" object and it has a boolean value "setUp". I can access to "setUp" and print its value with OnGUI from TouchClass but those condition blocks (if and else if) doesn' work. If i touch the "SlideMenu" object, it turns this "setUp" value to true and touch again its value turns into false. It's working as i want. So is there anyone who can tell me what i'm doing wrong? Why do these conditions not work?
So I reckon you built the game and tried on it on your device or you run it on the emulator.
Now how far do you get down in the ifs?
I can see you are missing } already but that could be a wrong typo:
for(int i = 0; i < Input.touchCount; i++)
{
print("In for");
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
print("In Hit");
if(!slider.setUp)
print("Set up");
else
print("No set up");
}
}
Answer by Kiwasi · Jul 11, 2014 at 11:02 PM
Add a Debug.Log(slider.setUp) at line 11. (ie inside the first if statement, but not the second)
Possible results
The console shows nothing. This means your outer if is not working
The console shows something that is not true or false. This means that setUp is not a bool
The console shows null. This means you haven't got the reference correctly, or it hasn't been initialised
The console shows true or false. This means its working fine and you have messed up the coding somewhere else
Answer by HolBol · Jul 11, 2014 at 11:03 PM
Well for starters you haven't closed the first if or the function, so you're missing two brackets. Also, you can just use else if you only need two cases rather than else if. Also, what is HitTest? Can't see where you've defined that either. We need a little more info about what you're trying to do first.
public class TouchClass : MonoBehaviour {
SliderScr slider;
void Start(){
slider = GameObject.Find("SlideMenu").GetComponent<SliderScr>();
}
void Update(){
for(int i = 0; i < Input.touchCount; i++)
{
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
if(!slider.setUp)
{
//Not working...
}
else
{
//Not working...
}
}
}
void OnGUI()
{
GUI.Label(new Rect(50,50,50,50),slider.setUp.ToString());
}
}
Thank you all dudes (especially Bored$$anonymous$$ormon), i figured it out; setUp value was null and i made correction to get reference right. It's working now...
Follow this Question
Related Questions
Help playing the right animation 1 Answer
Why am i getting this error?! 2 Answers
How to finish my else if statement? 2 Answers
For loop has 'if-else' impossibility. 2 Answers
I am getting error cs1525 for '}' on line 11,17. How come? 1 Answer