- Home /
Victory menu is not working HELP
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! \nYou 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
Answer by bobisgod234 · Jun 14, 2017 at 03:49 AM
The error is happening on this line:
victoryMenu.Victory();
Which suggests that victoryMenu is null. You attempt to assign it here:
victoryMenu = VictoryMenu.FindObjectOfType<VictoryMenu>();
Which suggests that Unity could not find an object of type VictoryMenu, which suggests that there is no actual gameobject with the VictoryMenu component attached to it anywhere in your scene.
Try typing "t:VictoryMenu" into the search bar above the hierarchy, and see what comes up. This will show you all objects with a component of type VictoryMenu attached. if nothing comes up, that is the problem.