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