How can I end and reset the game when 'Ball' collides with 'WallLeft'?
I am making a pong-type game. I want the game to end when 'Ball', collides with 'WallLeft'. I have tried many codes and scripts from my own knowledge, tutorials, forums, and unity answers but nothing I have tried has worked. Please help!
Answer by Denis115 · Aug 13, 2016 at 03:01 PM
Make sure your ball and wall have fitting colliders. For a 3D game a Sphere Collider for the ball and a Box Collider for the wall, for a 2D game a Circle Collider 2D and Box Collider 2D. Also make sure you have a game over scene in your game, if not create one.
Here you can see 2 cubes that i used for the wall and ball, and a script called "Test". Create a script like this (You didn't specify what language i use, i will show you in C#): https://s10.postimg.org/h7vrnw11l/image.png
Add a Rigidbody component (or rigidbody 2D) to the Wall object, and make sure that 'Use Gravity is not ticked. (https://s10.postimg.org/8qw9cywcp/image.png)
Add a Rigidbody component to the ball as well and make sure that 'Is Kinematic' is ticked. (https://s10.postimg.org/pfxp8vsy1/image.png)
You can easily detect the Wall by adding a tag to it. Select the wall object, go to Tag > Add tag, click the plus icon and type in Wall (https://s10.postimg.org/5mllg6fk9/image.png)
Now with the wall selected, go to tag, and select Wall (https://s10.postimg.org/cec0j14jt/image.png)
Now open the script you created earlier, and before doing anything, make sure you add using UnityEngine.SceneManagement;
at the top of your script.
Now put this code in:
void OnCollisionEnter(Collision exampleCol) {
if(exampleCol.collider.tag == "Wall")
{
//Replace 'Game Over' with your game over scene's name.
SceneManager.LoadScene("Game Over");
}
}
What this means is when the ball collides with something, if its tag is "Wall" , load the Game Over scene. And since we gave the wall object and only the wall object the "Wall" tag, the SceneManager.LoadScene code will only run when the ball collides with the wall.
Before this can work you need to add your scenes to the build settings. Go to file > build settings, drag all your scenes to the list, and make sure the order is correct. The main scene should be first in the list, and then the game over scene. (https://s10.postimg.org/hamohnk89/image.png)
Now attach the script to the ball and press play!
Additionaly, since you said you wanted 'the game to end when it collides with WallLeft', you could completely quit the game with this code:
void OnCollisionEnter(Collision exampleCol) {
if(exampleCol.collider.tag == "Wall")
{
Application.Quit();
}
}
The above code obviously won't work in the editor, but if you built the game and ran it, when this code ran, the game would've completely closed.
Answer by LK84 · Aug 13, 2016 at 10:39 AM
Depending on how you define "end and reset game" (just loading a scene?) you can just use OnCollisionEnter (drop this script to your Ball game Object):
void OnCollisionEnter(Collision wall)
{
if (wall.gameObject.name=="WallLeft")
{
//End and Reset Game whatever you need to do
}
}
Note, it's better to use tag insted of name, so you might wanna tag your WallLeft GameObject
Your answer
![](https://koobas.hobune.stream/wayback/20220612090723im_/https://answers.unity.com/themes/thub/images/avi.jpg)