İ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?

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

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());
        }
    
    }