Can't figure out why my code is "Unreachable"?

Solved, may be closed

These are the errors I get. My code within that range is the following:

RaycastHit ladderRayHit;
			if (Physics.Raycast(position, Vector3.down, out ladderRayHit, 1f))
			{
				//Is the object a Ladder?
				if (ladderRayHit.transform.tag == "Ladder")
				{
					//The object was a ladder
					return true;
					Debug.Log("The object bellow me was a ladder"); //Assets/Scripts/sPlayer.cs(280,47): warning CS0162: Unreachable code detected
				}
				else
				{
					//The object was not a ladder
					return false;
					Debug.Log("The object bellow me was NOT a ladder"); //Assets/Scripts/sPlayer.cs(286,47): warning CS0162: Unreachable code detected
				}
			}
			else
			{
				return false;
				Debug.Log("There was nothining bellow me"); //Assets/Scripts/sPlayer.cs(292,39): warning CS0162: Unreachable code detected

			}

I placed comments in the code so you can see where the warnings points at.
I’ve been thinking about this for about 30 minutes, but just can’t figure out why the heck that is unreachable.
It seems like there is something wrong with the raycast, but I don’t see why :S

This peice of code is quite important for other things in my script to work, so I need to know why it’s unreachable.

Here’s the full script if you for some reason need to know it: (Warning: long)

using UnityEngine;
using System.Collections;

public class sPlayer : MonoBehaviour {
	
	#region Variables
	
	public GameObject _Ladder;
	
	private float smoothTime = 0.15F;
    private Vector3 velocity = Vector3.zero;
	
	private float fallSpeed = 6f;
	
	private Vector2 MiddleOfScreen;
	
	private Vector3 myPosition;
	
	private Rect rightButton, leftButton, upButton, downButton, restartButton, ladderButton;
	#endregion
	
	// Use this for initialization
	void Start () {
		
		#region Buttons
		//Yupp, buttons
		MiddleOfScreen = new Vector2(Screen.width / 2, Screen.height / 2);
		
		rightButton	= new Rect(MiddleOfScreen.x + 50, MiddleOfScreen.y - 50, 100, 100);
		leftButton	= new Rect(MiddleOfScreen.x - 150, MiddleOfScreen.y - 50, 100, 100);
		upButton	= new Rect(MiddleOfScreen.x - 50, MiddleOfScreen.y - 150, 100, 100);
		downButton	= new Rect(MiddleOfScreen.x - 50, MiddleOfScreen.y + 50, 100, 100);
		
		restartButton = new Rect(20, 20, 60, 40);
		ladderButton = new Rect(Screen.width - 120, Screen.height - 80, 100, 60);
		#endregion
		
		//Sets myPosition to starting position
		myPosition = transform.position;
		
		//Makes the player able to use the keyboard if running on correct device
		if (Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.WindowsWebPlayer || Application.platform == RuntimePlatform.FlashPlayer)
		{
			gameObject.AddComponent("sPlayerKeyboard");
			Debug.Log("Running on PC");
		}
	}
	
	// Update is called once per frame
	void Update () {
		
		#region MoveLikeNormal
		//Moves the player to it's position along the x and y axis
		transform.position = Vector3.SmoothDamp(transform.position, new Vector3(myPosition.x, transform.position.y, myPosition.z), ref velocity, smoothTime);
		
		
		if (isThereALadder("down", myPosition))
		{
			transform.position = Vector3.SmoothDamp(transform.position, new Vector3(transform.position.x, myPosition.y, transform.position.z), ref velocity, smoothTime);
		}
		#endregion
		
		#region Gravity
		//If the player should fall
		if (myPosition.y < transform.position.y)
		{
			//If the player is not touching a ladder he should fall
			if (!isThereALadder("down", myPosition)  !isThereALadder("down", myPosition))
			{
				#region Fall
				//make player fall
				transform.Translate(Vector3.down * fallSpeed * Time.deltaTime, Space.World);
				
				//If the player is almost as the correct position, snap him to it
				if (Mathf.Round(myPosition.y * 10) == Mathf.Round(transform.position.y * 10))
				{
					transform.position = new Vector3(transform.position.x, myPosition.y, transform.position.z);
				}
				#endregion
			}
		}
		//If the player fell to far
		else if (isThereABlock("down", myPosition)  myPosition.y > transform.position.y)
		{
			//correct the position
			transform.position = new Vector3(transform.position.x, myPosition.y, transform.position.z);
		}
		#endregion
		
	}
	
	void FixedUpdate()
	{
		//Makes the player fall if there is no block under him.
		if (!isThereABlock("down", myPosition) 
			!isThereALadder("middle", myPosition) 
			!isThereALadder("down", myPosition))
		{
			myPosition += Vector3.down;
			Debug.Log("DER IS NO LADDER BELLOW MEH");
		}
	}
	
	//Makes the player move
	public void Move(string direction)
	{
		if (direction == "down")
		{
			if (isThereABlock(direction, myPosition))
			{
				RaycastHit moveRayHit;
				if (Physics.Raycast(myPosition, Vector3.down, out moveRayHit, 1))
				{
					moveRayHit.transform.GetComponent<sBlock>().Destroyed();
				}
			}
		}
		else if (direction == "left")
		{
			if (isThereABlock(direction, myPosition))
			{
				RaycastHit moveRayHit;
				if (Physics.Raycast(myPosition, Vector3.left, out moveRayHit, 1))
				{
					moveRayHit.transform.GetComponent<sBlock>().Destroyed();
				}
			}
			myPosition.x -= 1;
		}
		else if (direction == "right")
		{
			if (isThereABlock(direction, myPosition))
			{
				RaycastHit moveRayHit;
				if (Physics.Raycast(myPosition, Vector3.right, out moveRayHit, 1))
				{
					moveRayHit.transform.GetComponent<sBlock>().Destroyed();
				}
			}
			myPosition.x += 1;
		}
		else if (direction == "up")
		{
			if (isThereABlock(direction, myPosition))
			{
				Debug.Log("Block above");
				RaycastHit moveRayHit;
				if (Physics.Raycast(myPosition, Vector3.up, out moveRayHit, 1))
				{
					moveRayHit.transform.GetComponent<sBlock>().Destroyed();
				}
			}
			if (isThereALadder("middle", myPosition))
			{
				Debug.Log("Can go up");
				myPosition.y += 1;
			}
		}
	}
	
	public bool isThereABlock(string direction, Vector3 position)
	{
		RaycastHit isThereABlockRayHit;
		if (direction == "down")
		{
			if (Physics.Raycast(position, Vector3.down, out isThereABlockRayHit, 1))
			{
				if (isThereABlockRayHit.transform.tag == "Block")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		else if (direction == "left")
		{
			if (Physics.Raycast(position, Vector3.left, out isThereABlockRayHit, 1))
			{
				if (isThereABlockRayHit.transform.tag == "Block")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		else if (direction == "right")
		{
			if (Physics.Raycast(position, Vector3.right, out isThereABlockRayHit, 1))
			{
				if (isThereABlockRayHit.transform.tag == "Block")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		else if (direction == "up")
		{
			if (Physics.Raycast(position, Vector3.up, out isThereABlockRayHit, 1))
			{
				if (isThereABlockRayHit.transform.tag == "Block")
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			Debug.LogError(direction + " is not a valid string for isThereABlock!");
			return false;
		}
	}
	public bool isThereALadder(string direction, Vector3 position)
	{
		if (direction == "middle")
		{
			#region Middle
			RaycastHit ladderRayHit;
			position = new Vector3(position.x, position.y, position.z + 1); //Replace position so that the Raycast will work
			if (Physics.Raycast(position, Vector3.back, out ladderRayHit, 1f))
			{
				//Is the object a Ladder?
				if (ladderRayHit.transform.tag == "Ladder")
				{
					//The object was a ladder
					return true;
				}
				else
				{
					//The object was not a ladder
					return false;
				}
			}
			else
			{
				return false;
			}
			#endregion
		}
		else if (direction == "down")
		{
			#region Down
			RaycastHit ladderRayHit;
			if (Physics.Raycast(position, Vector3.down, out ladderRayHit, 1f))
			{
				//Is the object a Ladder?
				if (ladderRayHit.transform.tag == "Ladder")
				{
					//The object was a ladder
					return true;
					Debug.Log("The object bellow me was a ladder");
				}
				else
				{
					//The object was not a ladder
					return false;
					Debug.Log("The object bellow me was NOT a ladder");
				}
			}
			else
			{
				return false;
				Debug.Log("Der was nothining bellow me");
			}
			#endregion
		}
		else
		{
			#region Error
			Debug.LogError(direction + " is not a valid string for isThereALadder!");
			return false;
			#endregion
		}
	}
	
	void OnGUI()
	{
		if (GUI.Button(rightButton, "", GUIStyle.none))
		{
			Move("right");
		}
		if (GUI.Button(leftButton, "", GUIStyle.none))
		{
			Move("left");
		}
		if (GUI.Button(upButton, "", GUIStyle.none))
		{
			Move("up");
		}
		if (GUI.Button(downButton, "", GUIStyle.none))
		{
			Move("down");
		}
		
		if (GUI.Button(restartButton, "Restart"))
		{
			Application.LoadLevel(Application.loadedLevel);
		}
		if (GUI.Button(ladderButton, "Ladder"))
		{
			Instantiate(_Ladder,
				new Vector3(myPosition.x, Mathf.Round(myPosition.y), myPosition.z + .5f), //Places the Ladder in the correct position
				Quaternion.Euler(new Vector3(-90, 0, 0)));
		}
		
	}
	
	void OnDrawGizmos()
	{
		Gizmos.color = Color.yellow;
		Gizmos.DrawSphere(myPosition, .2f);
	}
}

Any help would be appreciated :slight_smile:
Also, sorry if I make any obvious mistakes. I’m quite new to C# but I’m starting to get the hang of it :smile:

because you have return there
return will terminate the function at that point and whatever else came after will not be executed.
all your 3 debug logs are useless in there cause they will never be called → unreachable

You have return statement before calling method Debug.Log
Debug.Log(…) is unreachable in that cases
Comment these Debug.Log lines or put them before return

Oh, I didn’t know that return statements worked that way. Makes sense now!
Thanks guys :slight_smile:

Hello Everyone,

I do not have any return statement in my code still I am facing the same problem.
For your reference my code is here:

I have add warning as a comment after the line.

using UnityEngine;
using System.Collections;

public class PlayerInput : MonoBehaviour 
{
	public GameObject plus_1;
	public GameObject plus_2;
	public GameObject plus_3;
	public GameObject plus_4;
	public GameObject minus_5;
	public GameObject bullet;
	private GameObject bulletShell;
	private GameObject hit_Obj;
	public GameObject bg_Plane;
	public Transform[] SubobjectPrefabs;
	
	private Transform newObj;
	
	private Ray ray;
	private Touch touch;
	
	public Material grayMat_Square;
	public Material grayMat_Round;
	
	private bool showScore = false;
	private bool canFire = false;
	public bool remindReload = false;
	
	private string hiObj_Name;
	
	private Vector3 hitObj_Pos;
	private Vector3 bulletInstantiationPos;
	
	private GameManager GM;
	
	private int random;
	public int bulletLeft = 7;
	public float moveSpeed = 0.5f;
	private float sw;
	private float sh;
	
	public Texture2D reloadInActive;
	
	private DirectionEnum spawnDirection;
	
	void Start()
	{
		GM = GameObject.Find("Main Camera").GetComponent<GameManager>();
		sw = Screen.width;
		sh = Screen.height;
	}
	
	void Update () 
	{
		if (Time.frameCount % 20 == 0)
    	{
			System.GC.Collect();
	    }
		if(!GM.isPauseClicked)
		{
		    if(Input.GetMouseButtonDown(0))
			{
				if(bulletLeft > 0)
				{
		            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		            RaycastHit hit;
		            if (Physics.Raycast (ray, out hit, 100)) 
					{
						hit_Obj = hit.collider.gameObject;
		                if (hit_Obj.tag == "ShootingObject")
		                {
							hiObj_Name = hit_Obj.name;
							hitObj_Pos = hit_Obj.transform.position;
							showScore = true;
							ShowScore();
							if(hit_Obj.layer == 8)
							{
								hit_Obj.renderer.material = grayMat_Round;
							}
							else if(hit_Obj.layer == 9)//access child of this object and change material of that only
							{
								hit_Obj.renderer.material = grayMat_Square;
								
								Renderer[] children = hit_Obj.GetComponentsInChildren<Renderer>();
		 
								foreach ( Renderer i in children)
								{
									i.material = grayMat_Square;
								}
							}
							hit_Obj.tag = "None";
							GameManager.SP.RemoveObject(hit.transform);
							//GM.ResetMissedTarget();
		                }
						
						bulletInstantiationPos = hit.point;
						if(bulletInstantiationPos.y < -3.6)
						{
							//do nothing
						}
						else
						{
							if(!canFire)
							{
								canFire = true;
								random = Random.Range(0,4);
								if(random >= 3)
									spwanSubTarget();
								fire();
							}
						}
					}
	            }
				else
				{
					if(PlayerPrefs.GetString("_ksound_Enabled") == "Yes")
						bg_Plane.audio.Play();
					remindReload = true;
				}					
	        }
			else if(Input.GetMouseButtonUp(0))
				remindReload = false;
		}
		else
			Time.timeScale = 0.0f;
	}
	
	void OnGUI()
	{
		if(remindReload)
			GUI.DrawTexture( new Rect(sw/1.225f,sh/1.125f,sw/6.5f,sh/12),reloadInActive);//Reload Button
		if(newObj)
		Debug.Log("in PI -> newObj pos = " +newObj.position);
	}
	
	void ShowScore()
	{
		if(showScore)
		{
			showScore = false;
			switch(hiObj_Name)
			{
				case "RedRound_Terget(Clone)":
				case "RedSquare_Terget(Clone)":
					Instantiate(plus_1,new Vector3(hitObj_Pos.x,hitObj_Pos.y,11.25f),Quaternion.Euler(0,90,0));
					break;
				case "GreenBig_Target(Clone)":
				case "GreenSmall_Target(Clone)":
					Instantiate(plus_2,new Vector3(hitObj_Pos.x,hitObj_Pos.y,11.25f),Quaternion.Euler(0,90,0));
					break;
				case "Purple_Target(Clone)":
					Instantiate(plus_3,new Vector3(hitObj_Pos.x,hitObj_Pos.y,11.25f),Quaternion.Euler(0,90,0));
					break;
				case "Yellow_target(Clone)":
					Instantiate(plus_4,new Vector3(hitObj_Pos.x,hitObj_Pos.y,11.25f),Quaternion.Euler(0,90,0));
					break;
				case "Smiley(Clone)":
					Instantiate(minus_5,new Vector3(hitObj_Pos.x,hitObj_Pos.y,11.25f),Quaternion.Euler(0,90,0));
					break;
			}
		}
	}
	
	void fire()
	{
		if(canFire)
		{
			canFire = false;
			bulletShell = (GameObject)Instantiate(bullet,new Vector3(bulletInstantiationPos.x,bulletInstantiationPos.y,11.0f),Quaternion.Euler(0,180,0));
			if(PlayerPrefs.GetString("_ksound_Enabled") == "Yes")
				bulletShell.audio.Play();
			bulletShell.rigidbody.useGravity = true;
			GM.RemoveBullet();
			bulletLeft--;
		}
	}
	
	void spwanSubTarget()
	{
		switch((int)Random.Range(0,5))
		{
			case 1:
				spawnDirection = DirectionEnum.leftDown;
				break;
			case 2:
				spawnDirection = DirectionEnum.leftUp;
				break;
			case 3:
				spawnDirection = DirectionEnum.rightDown;
				break;
			case 4:
				spawnDirection = DirectionEnum.rightUp;
				break;
		}
		
		DirectionEnum direction = spawnDirection;
		switch(hiObj_Name)
		{
			case "RedRound_Terget(Clone)":
				newObj = (Transform)Instantiate(SubobjectPrefabs[0],hitObj_Pos,Quaternion.Euler(0,90,0));
				break;
			case "GreenBig_Target(Clone)":
				newObj = (Transform)Instantiate(SubobjectPrefabs[1],hitObj_Pos,Quaternion.Euler(0,90,0));
				break;
			case "Purple_Target(Clone)":
				newObj = (Transform)Instantiate(SubobjectPrefabs[2],hitObj_Pos,Quaternion.Euler(0,90,0));
				break;
			
			MovingObject movObj = new MovingObject(direction, newObj);//warning CS0162: Unreachable code detected	
			GameManager.SP.AddTarget( movObj );
		}
	}
}

I have this MovingObject class in another script that comes in the scene with a prefab.
Thanks for the help in advance!

Regards:
Niki.j

just move the two lines outside of the switch statement

Sorry for the wrong post.
the two lines are already out of the switch statement.

Thanks
Niki.j

James,

Sorry man, you are right, it was so silly of me.
I am really very sorry for this kind of stupid post.

Regards:
Niki.j