Search Unity

(Solved!) Code For Death Menu-Need Help

Discussion in 'Scripting' started by Nikola310, Nov 29, 2015.

  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hello,everyone.
    I am trying to make a death menu(that appears after you die) and have almost succeeded but there's one problem.
    I've made a Text saying "You died!" and two buttons,one is restart and the other is Quit to main menu.
    I want to make it so that when you press "Restart" the level restarts,my points get reset to 0 and my ball(player) goes to the start location where it spawns normally.

    The quit to main menu button works,but upon pressing restart I get an error:

    "NullReferenceException: Object reference not set to an instance of an object
    DeathMenu.RestartGame () (at Assets/Scripts/DeathMenu.cs:13)"

    Here is my Death Menu script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DeathMenu : MonoBehaviour
    5. {
    6.     void Start ()
    7.     {
    8.  
    9.     }
    10.  
    11.     public void RestartGame()
    12.     {
    13.         FindObjectOfType<PlayerController> ().Reset ();
    14.     }
    15.  
    16.     public void QuitToMain()
    17.     {
    18.         Application.LoadLevel("MainMenu");
    19.     }
    20. }
    And here is my PlayerController script(all though I should rename it,because it is in the player and used for it's movement but also for other things):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.    
    7.     public float speed;
    8.     public Text CountText;
    9.     public static int count;
    10.     public Text WinText;
    11.     public AudioClip[] audioClip;
    12.     public PlayerController ThePlayer;
    13.     private Vector3 StartPosition;
    14.     private Quaternion StartRotation;
    15.     public DeathMenu TheDeathScreen;
    16.     public Text tip;
    17.  
    18.     void Awake ()
    19.     {
    20.     }
    21.  
    22.     void Start ()
    23.     {
    24.         count = 0;
    25.         SetCountText ();
    26.         StartPosition = transform.position;
    27.         StartRotation = transform.rotation;
    28.     }
    29.     void FixedUpdate ()
    30.     {
    31.         float moveHorizontal = Input.GetAxis ("Horizontal");
    32.         float moveVertical = Input.GetAxis ("Vertical");
    33.  
    34.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    35.  
    36.         GetComponent<Rigidbody>().AddForce  (movement * speed);
    37.  
    38.     }  
    39.     public void Reset ()
    40.     {
    41.         count = 0;
    42.         ThePlayer.transform.position = StartPosition;
    43.         TheDeathScreen.gameObject.SetActive (false);
    44.         ThePlayer.gameObject.SetActive (true);
    45.     }
    46.  
    47.  
    48.     public void OnTriggerEnter(Collider other)
    49.     {
    50.            if (other.gameObject.CompareTag ("PickUp")) {
    51.  
    52.             PlaySound (1);
    53.             other.gameObject.SetActive (false);
    54.             count = count + 1;
    55.             SetCountText ();
    56.             }
    57.  
    58.         if (other.gameObject.CompareTag ("Wall"))
    59.         {
    60.             PlaySound (0);
    61.             other.gameObject.SetActive (false);
    62.             count = count - 1;
    63.             SetCountText ();
    64.         }
    65.     }
    66.  
    67.     public void PlaySound(int clip)
    68.     {
    69.         GetComponent<AudioSource>().clip = audioClip[clip];
    70.         GetComponent<AudioSource>().Play ();
    71.  
    72.     }
    73.     public void SetCountText ()
    74.     {
    75.  
    76.         CountText.text = "Points: " + count.ToString ();
    77.         if (count >= 6)
    78.         {
    79.             //Application.LoadLevel ("Level2");
    80.             Application.LoadLevel(Application.loadedLevel + 1);
    81.         }
    82.         if (count <= -2)
    83.         {
    84.             ThePlayer.gameObject.SetActive (false);
    85.             TheDeathScreen.gameObject.SetActive (true);
    86.             tip.gameObject.SetActive (false);
    87.         }
    88.     }
    89.     /*void MinusCountLose ()
    90.     {
    91.         if (count <=-2)
    92.         {
    93.             ThePlayer.gameObject.SetActive (false);
    94.             TheDeathScreen.gameObject.SetActive (true);
    95.         }
    96.     } */
    97. }
    98.  
    99.  
    Thank you,in advance :)
     
  2. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DeathMenu : MonoBehaviour
    5. {
    6.     void Start ()
    7.     {
    8.        
    9.     }
    10.    
    11.     public void RestartGame()
    12.     {
    13.         PlayerController thePlayer  = FindObjectOfType(typeof(PlayerController)) as PlayerController;
    14.         thePlayer.Reset();
    15.  
    16.     }
    17.    
    18.     public void QuitToMain()
    19.     {
    20.         Application.LoadLevel("MainMenu");
    21.     }
    22. }
     
  3. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hmm..nope..Still getting the same error :(
     
  4. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    you destroy the player when die?
     
  5. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Yes,he should be destroyed upon dying.Collecting two cylinders kills you,because one cylinder is -1 point,and -2 points or even less destroys the player and makes you lose and gives the "Death screen".As I said,I want the restart button to work.
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If you are totally resetting everything when the player dies just reload that scene.

    I think it is something like
    Application.LoadLevel(Application.LoadedLevel);
     
  7. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Wow,thank you!Such a simple solution,how stupid of me not to think of that!Thank you :)
    P.S. It's loadedLevel :D
     
  8. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    No worries, I use it all the time when I'm prototyping so I don't need gui etc at the early stages.
     
  9. woodsynzl

    woodsynzl

    Joined:
    Apr 8, 2015
    Posts:
    156
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class DeathMenu : MonoBehaviour
    4. {
    5.     void Start ()
    6.     {
    7.     }
    8.     public void RestartGame()
    9.     {
    10.         FindObjectOfType<PlayerController> ().Reset ();
    11.     }
    12.     public void QuitToMain(int level)
    13.     {
    14.         Application.LoadLevel(level);
    15.     }
    16. }
    Just for easier use, try using this code instead of hardcoding in the level