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. Dismiss Notice

[Solved] How To Call GameOver Script On Player Death

Discussion in 'Scripting' started by GhulamJewel, Jan 3, 2015.

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Hi there I have a simple question. I been having trouble calling this Game Over script


    Code (CSharp):
    1. ing UnityEngine;
    2.  
    3. /// <summary>
    4. /// Start or quit the game
    5. /// </summary>
    6. public class GameOverScript : MonoBehaviour
    7. {
    8.     private GUISkin skin;
    9.  
    10.    
    11.     void Start()
    12.     {
    13.         // Load a skin for the buttons
    14.         skin = Resources.Load("GUISkin") as GUISkin;
    15.     }
    16.  
    17.     void OnGUI()
    18.     {
    19.         const int buttonWidth = 170;
    20.         const int buttonHeight = 80;
    21.  
    22.         // Set the skin to use
    23.         GUI.skin = skin;
    24.        
    25.         if (
    26.             GUI.Button(
    27.             // Center in X, 1/3 of the height in Y
    28.             new Rect(
    29.             Screen.width / 2 - (buttonWidth / 2),
    30.             (1 * Screen.height / 3) - (buttonHeight / 2),
    31.             buttonWidth,
    32.             buttonHeight
    33.             ),
    34.             "Retry"
    35.             )
    36.             )
    37.         {
    38.    
    39.             // Reload the level
    40.             Application.LoadLevel("Game");
    41.  
    42.            
    43.         }
    44.        
    45.         if (
    46.             GUI.Button(
    47.             // Center in X, 2/3 of the height in Y
    48.             new Rect(
    49.             Screen.width / 2 - (buttonWidth / 2),
    50.             (2 * Screen.height / 3) - (buttonHeight / 2),
    51.             buttonWidth,
    52.             buttonHeight
    53.             ),
    54.             "Exit"
    55.             )
    56.             )
    57.         {
    58.  
    59.             // Reload the level
    60.             Application.LoadLevel("TitleScreen");
    61.  
    62.            
    63.         }
    64.     }
    65.   }
    66.  
    67.  

    I attached this script to a empty game object and disable it so it does not show on load. Then I make another script below and add it to the player. So when player dies want to get the script above to show! But does not work! Get

    "
    NullReferenceException: Object reference not set to an instance of an object
    CallGameOver.OnDestroy () (at Assets/UI Scripts/GameOver/CallGameOver.cs:11) "


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CallGameOver : MonoBehaviour
    5. {
    6.  
    7.  
    8.         void OnDestroy ()
    9.         {
    10.  
    11.                 transform.parent.gameObject.AddComponent<GameOverScript> ();
    12.  
    13.         }
    14. }
    15.  

    I also tried below but did not seem to work and same error. Please advise thank you very much.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CallGameOver : MonoBehaviour
    5. {
    6.  
    7.  
    8.         void OnDestroy ()
    9.         {
    10.  
    11.                GameObject.Find("GameOver").active = true;
    12.  
    13.         }
    14. }
    15.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You want to use a public variable for the gameObject with the script and drag that onto it in the editor in the player script:
    public GameObject gameOverObject;

    The in on destroy, you get the script from that object using GetComponent:
    gameOverObject.GetComponent<GameOverScript>().enabled = true;
     
    GhulamJewel likes this.
  3. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Starting to notice you. I see that you are active in helping people and notice that you reply to many questions. A great help to the community. Thank you for your prompt and easy to understand answer and it worked great. :)
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Glad I could help.
     
    GhulamJewel likes this.