(Solved!) Code For Death Menu-Need Help

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:

using UnityEngine;
using System.Collections;

public class DeathMenu : MonoBehaviour
{
    void Start ()
    {

    }

    public void RestartGame()
    {
        FindObjectOfType<PlayerController> ().Reset ();
    }

    public void QuitToMain()
    {
        Application.LoadLevel("MainMenu");
    }
}

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):

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour {
   
    public float speed;
    public Text CountText;
    public static int count;
    public Text WinText;
    public AudioClip[] audioClip;
    public PlayerController ThePlayer;
    private Vector3 StartPosition;
    private Quaternion StartRotation;
    public DeathMenu TheDeathScreen;
    public Text tip;

    void Awake ()
    {
    }

    void Start ()
    {
        count = 0;
        SetCountText ();
        StartPosition = transform.position;
        StartRotation = transform.rotation;
    }
    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        GetComponent<Rigidbody>().AddForce  (movement * speed);

    }  
    public void Reset ()
    {
        count = 0;
        ThePlayer.transform.position = StartPosition;
        TheDeathScreen.gameObject.SetActive (false);
        ThePlayer.gameObject.SetActive (true);
    } 


    public void OnTriggerEnter(Collider other)
    {
           if (other.gameObject.CompareTag ("PickUp")) {

            PlaySound (1);
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
            }

        if (other.gameObject.CompareTag ("Wall"))
        {
            PlaySound (0);
            other.gameObject.SetActive (false);
            count = count - 1;
            SetCountText ();
        }
    }

    public void PlaySound(int clip)
    {
        GetComponent<AudioSource>().clip = audioClip[clip];
        GetComponent<AudioSource>().Play ();

    }
    public void SetCountText ()
    {

        CountText.text = "Points: " + count.ToString ();
        if (count >= 6)
        {
            //Application.LoadLevel ("Level2");
            Application.LoadLevel(Application.loadedLevel + 1);
        }
        if (count <= -2)
        {
            ThePlayer.gameObject.SetActive (false);
            TheDeathScreen.gameObject.SetActive (true);
            tip.gameObject.SetActive (false);
        }
    }
    /*void MinusCountLose ()
    {
        if (count <=-2)
        {
            ThePlayer.gameObject.SetActive (false);
            TheDeathScreen.gameObject.SetActive (true);
        }
    } */
}

Thank you,in advance :slight_smile:

try this:

using UnityEngine;
using System.Collections;

public class DeathMenu : MonoBehaviour
{
    void Start ()
    {
       
    }
   
    public void RestartGame()
    {
        PlayerController thePlayer  = FindObjectOfType(typeof(PlayerController)) as PlayerController;
        thePlayer.Reset();

    }
   
    public void QuitToMain()
    {
        Application.LoadLevel("MainMenu");
    }
}

Hmm…nope…Still getting the same error :frowning:

you destroy the player when die?

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.

If you are totally resetting everything when the player dies just reload that scene.

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

Wow,thank you!Such a simple solution,how stupid of me not to think of that!Thank you :slight_smile:
P.S. It’s loadedLevel :smile:

No worries, I use it all the time when I’m prototyping so I don’t need gui etc at the early stages.

using UnityEngine;
using System.Collections;
public class DeathMenu : MonoBehaviour
{
    void Start ()
    {
    }
    public void RestartGame()
    {
        FindObjectOfType<PlayerController> ().Reset ();
    }
    public void QuitToMain(int level)
    {
        Application.LoadLevel(level);
    }
}

Just for easier use, try using this code instead of hardcoding in the level