How to put an if statement inside an if statement?

Hi. This is my code which does not seem to work, can you please tell me how to make it work?

//                         if (Input.GetMouseButtonDown(0)){
//				float ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//				RaycastHit hit;
//				if(Physics.Raycast(ray,hit,Mathf.Infinity){
//					if (hit.collider.name=="hitbox1")
//						Application.loadedLevel("level1");
//
//					if (hit.collider.name=="hitbox2")
//						Application.loadedLevel("level2");
//					}
//					if (hit.collider.name=="hitbox3"){
//						Application.loadedLevel("level3");
//				}
//					if (hit.collider.name=="hitbox4"){
//						Application.loadedLevel("level4");
//				}
//					if (hit.collider.name=="hitbox5"){
//						Application.loadedLevel("level5");
//				}
//					if (hit.collider.name=="hitbox6"){
//						Application.loadedLevel("level6");
//				}
//					if (hit.collider.name=="hitbox7"){
//						Application.loadedLevel("level7");
//				}
//					if (hit.collider.name=="hitbox8"){
//						Application.loadedLevel("level8");
//				}
//					if (hit.collider.name=="hitbox9"){
//						Application.loadedLevel("level9");
//				}
//					if (hit.collider.name=="hitbox10"){
//						Application.loadedLevel("level10");
//				}
//					if (hit.collider.name=="hitbox11"){
//						Application.loadedLevel("level11");
//				}
//					if (hit.collider.name=="hitbox12"){
//						Application.loadedLevel("level12");
//				}
//					if (hit.collider.name=="hitbox13"){
//						Application.loadedLevel("level13");
//				}
//					if (hit.collider.name=="hitbox14"){
//						Application.loadedLevel("level14");
//				}
//					if (hit.collider.name=="hitbox15"){
//						Application.loadedLevel("level15");
//				}
//					if (hit.collider.name=="hitbox16"){
//						Application.loadedLevel("level16");
//				}
//					if (hit.collider.name=="hitbox17"){
//						Application.loadedLevel("level17");
//				}
//					if (hit.collider.name=="hitbox18"){
//						Application.loadedLevel("level18");
//				}
//					if (hit.collider.name=="hitbox19"){
//						Application.loadedLevel("level19");
//					}
//					if (hit.collider.name=="hitbox20"){
//						Application.loadedLevel("level20");
//					}
//					
//
//				}

You have missed curly brackets in the first two nested ifs.
Change them to this:

if (hit.collider.name=="hitbox1"){ Application.loadedLevel("level1"); } if (hit.collider.name=="hitbox2"){ Application.loadedLevel("level2"); }

Or replace all of them since the strings are so similar. Just replace hitbox with level.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            string hitbox = hit.collider.name;
            if (hitbox.Contains("hitbox"))
            {
                string level = hitbox.Replace("hitbox", "level");
                Application.LoadLevel(level);
            }
        }
    }
}

Hi there xyz567 is right you’ve missed some curly braces. Although might I suggest using a switch case statement rather than using so many If’s? For example you could do this instead.

if(Input.GetMouseButtonDown(0))
		{
			float ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if(Physics.Raycast(ray,hit,Mathf.Infinity))
			{
				switch (hit.collider.name)
				{
				case "hitbox1":
					Application.LoadLevel("level1");
					break;
				case "hitbox2":
					Application.LoadLevel("level2");
					break;
				case "hitbox3":
					Application.LoadLevel("level3");
					break;
				case "hitbox4":
					Application.LoadLevel("level4");
					break;
				case "hitbox5":
					Application.LoadLevel("level5");
					break;
				case "hitbox6":
					Application.LoadLevel("level6");
					break;
				case "hitbox7":
					Application.LoadLevel("level7");
					break;
				case "hitbox8":
					Application.LoadLevel("level8");
					break;
				case "hitbox9":
					Application.LoadLevel("level9");
					break;
				case "hitbox10":
					Application.LoadLevel("level10");
					break;
				case "hitbox11":
					Application.LoadLevel("level11");
					break;
				case "hitbox12":
					Application.LoadLevel("level12");
					break;
				case "hitbox13":
					Application.LoadLevel("level13");
					break;
				case "hitbox14":
					Application.LoadLevel("level14");
					break;
				case "hitbox15":
					Application.LoadLevel("level15");
					break;
				case "hitbox16":
					Application.LoadLevel("level16");
					break;
				case "hitbox17":
					Application.LoadLevel("level17");
					break;
				case "hitbox18":
					Application.LoadLevel("level18");
					break;
				case "hitbox19":
					Application.LoadLevel("level19");
					break;
				case "hitbox20":
					Application.LoadLevel("level20");
					break;
				}
			}
		}

It’s just a suggestion but it could help you performance wise and it can also be easier to read in the long run.