I asked this question yesterday, but it got closed by someone and the person gave me a link that I throughly read but it didn’t help me. So here I go again…
I’m making my very first game and I’ve just completed my first level in it, however I need a victory menu but it is not working for me. Everytime I reach the required score to beat the level, it crashes and gives me the following error: NullReferenceException: Object reference not set to an instance of an object
PointsCounter.Update () (at Assets/Scripts/PointsCounter.cs:39)
Here’s my scripts:
using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
using UnityEngine.SceneManagement;
public class PointsCounter : MonoBehaviour {
public UnityEngine.UI.Text pointsDisplay;
public AudioClip coinPickUp;
private VictoryMenu victoryMenu;
private AudioSource source;
public int amountOfPoints;
private void Awake()
{
source = GetComponent<AudioSource>();
}
void Start()
{
victoryMenu = VictoryMenu.FindObjectOfType<VictoryMenu>();
pointsDisplay.text = "Points: " + amountOfPoints;
}
public void AddPoints(int points)
{
source.PlayOneShot(coinPickUp);
amountOfPoints += points;
pointsDisplay.text = "Points: " + amountOfPoints;
}
private void Update()
{
if (amountOfPoints >= 1000)
{
Debug.Log("Victory! You completed the level.");
victoryMenu.Victory();
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class VictoryMenu : MonoBehaviour {
public Text victoryMessage;
public Text nextLevel;
public Text restartLevel;
public Text quitGame;
private void Start()
{
gameObject.SetActive(false);
}
public void Victory()
{
victoryMessage.text = "Wohoo!
You completed the level!";
nextLevel.text = “NEXT LEVEL”;
restartLevel.text = “RESTART”;
quitGame.text = “QUIT GAME”;
gameObject.SetActive(true);
}
public void LoadNextLevel()
{
SceneManager.LoadScene("Scene2");
}
public void RestartCurrentLevel()
{
SceneManager.LoadScene("Scene1");
}
public void QuitGame()
{
Application.Quit();
}
}
Please don’t just close this question as last time, give me some advice instead because I am tired of this… Been trying for hours now with no success