Wall checking not working correctly

Basically I am working on side scrolling 2d game. For that I want to check whether player is collided with wall. I have written some code for it but can’t able to decide where I have done stupid mistake.
So please guide me in this. If you need any more details then I am always available.

Code for detecting wall :

            RaycastHit2D hit = Physics2D.Raycast (wallCheck.position, Vector2.right, wallCheckDistance);

		if (Physics2D.Raycast (wallCheck.position, Vector2.right, wallCheckDistance)) {
			Debug.Log ("collider name : " + hit.collider.name);
			isDead = true;
		}

		Debug.DrawRay (wallCheck.position, Vector3.right * wallCheckDistance, Color.red);

Following image gives you all idea about situation. In that I have allocated player with groundCheck and wallCheck flag. At present in wallCheck section, I am detecting ground as collider response after execution of above code. Above code I have placed in Update method for continuous checking.

OK< I think I get what is being asked. The question is put wrong though.

The question is how does he set the collision to detect and when colliding with said object to cause it to more or less kill the player as a Game Over Notice. The problem is he is using a Debug checker for detection of collision notice instead f calling the function when actually making the contact.

siddharth3322, you don’t use collision detection that way. Collision detection is simply to make sure your character is actually interacting with all the objects in the environment and the debug part is to simply give you a notice to tell you the detection and interactions are working or not.

You need to create script like DestroyByContact that then after your character is “Killed/destroyed” by a hazard object, enemy, or weapon, it displays a Game Over notice in the UI (User Interface), such as when setting up a menu with a canvas Panel and having the script pull that notice.

Otherwise collision detection is just that and nothing more than that. Its not for destroying object or putting up Game Over UI elements. The Debug is only to tell you if something is working or not working.

These tutorial may be closer to what you are wanting to do.

https://www.youtube.com/watch?v=9z7_tlX8OTg

I do not know though what version of Unity is being used, so the first one is older using JavaScript and 4.6. while the second is more current using C# and a version of Unity5.

I think it should be like this because you are no where comparing whether hitting a wall or any other object.

 RaycastHit2D hit = Physics2D.Raycast (wallCheck.position, Vector2.right, wallCheckDistance);
 
         if (hit.collider.name =="Your Wall name")) {
             Debug.Log ("collider name : " + hit.collider.name);
             isDead = true;
         }