Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Basic game over screen

Discussion in 'Scripting' started by Bountyhunter678, Jun 28, 2022.

  1. Bountyhunter678

    Bountyhunter678

    Joined:
    Feb 17, 2020
    Posts:
    3
    Hi!

    I'm trying to make a simple 3d unity horror game and part of that game entails having a creature run after you and try to kill you.


    What I want to know is that when the player's capsule collides with the creature, how do I make that transition to a game over screen with a retry button?

    (Note: The only thing I have so far is a cube to be a test enemy and test the game over function by running into it. It currently does not move.)
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You can use the SceneManager to call things like LoadScene to just load a Game Over scene. Then, when you click the retry button, you just load your game scene again.
     
    Kurt-Dekker likes this.
  3. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    First create a canvas, create an empty gameobject + add text "Game over" or something else, you can add also an image, add all of this to the empty gameobject. then disable this gameobject.
    create a new script :
    add this :
    Code (CSharp):
    1. [SerializeField] GameObject gameOverPanel;
    2.  
    3. public void OnTriggerEnter(Collider other)
    4. {
    5. var player = other.getComponent<Player>();
    6.  
    7. if(player != null)
    8. gameobject.setactive(true);
    9.  
    10. }
    Cheers
     
    bplc likes this.
  4. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    RadRedPanda explains to you how to create the code that you will indicate to your button, so that when you click on it the game restarts.

    TheDevloper explains how to create the code, so that when your enemy collides with your player, your restart UI appears on the screen.


    Youtube is filled with videos explaining how to do it.
    It's very easy.

    I also advise you to do the Unity learn exercises.
     
  5. Bountyhunter678

    Bountyhunter678

    Joined:
    Feb 17, 2020
    Posts:
    3
    when I tried using this script, there were a lot of errors and I couldnt add it to the game object. These are the errors I got. Screenshot 2022-06-29 004017.png
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    What @TheDevloper posted was a tiny fraction of a complete script to exhibit the concept. It's sort of like me telling you "when you get here, come up the left driveway." It won't help you find the house but it could still be useful.

    As I posted to you here:

    https://forum.unity.com/threads/enemy-ai.1301448/#post-8238384

    Go do tutorials, do lots and lots of them and do them properly. EVERY single character has to be typed, not just an abbreviated conceptual example like TheDevloper posted, which is a great example, assuming you know how to script and have a script set up.
     
    TheDevloper and bplc like this.
  7. bplc

    bplc

    Joined:
    Mar 10, 2022
    Posts:
    104
    Yes, if your script only contains this, it may cause a problem. I think you are missing some fundamentals, I advise you to follow the unity learn training.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class nameOfMyScript : MonoBehaviour    // here nameOfMyScript = name of your script
    5. {  
    6.     public GameObject canvas; // here go in inspector, canvas = UI game Over
    7.    
    8.    
    9.     private void OnTriggerEnter(Collider myCollider) //Check mark, in inspector collider "is Trigger"
    10.     {
    11.         if(myCollider.gameObject.CompareTag("myTag")) //Here myTag = tag to add in other Object collision.
    12.            
    13.         {            
    14.             canvas.gameObject.SetActive(true);          
    15.         }
    16.     }
    17.    
    18.    
    19.    
    20. }
    21. //Add script on enemy or player, create and affiliate tag, those that do not contain the script.
    22. //This script will enable UI, so disable it.