Hello Guys, i need a help with a problem that i`m having.
So, when i start the game everything is running normally, then i have a game over panel, that appears in the screen with a simple button restart. When the scene is reloaded, suddently i have the problem.
NullReferenceException: Object reference not set to an instance of an object
GameManager.Carrega (UnityEngine.SceneManagement.Scene cena, UnityEngine.SceneManagement.LoadSceneMode modo) (at Assets/Scripts/GameManager.cs:61)
UnityEngine.SceneManagement.SceneManager.Internal_SceneLoaded (UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode) (at C:/buildslave/unity/build/Runtime/Export/SceneManager/SceneManager.cs:244)
And them they show me this 2 lines above:
denovo = GameObject.Find(“denovo”).GetComponent();
denovo.onClick.AddListener(Jogarnovamente);
and i checked everything there is no missing instance or reference…
full code above;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using Fungus;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
//SpawnJason
[SerializeField]
private int personagem;
private Transform spawn;
enum character
{
personagem = 0,
};
//
public static GameManager instance;
public bool gameOver;
[SerializeField]
private GameObject youLosePanel;
private Button denovo;
public bool jogoComecou;
//public GameObject[ ] uiElementos;
//public GameObject container1;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
}
else
{
Destroy(gameObject);
}
SceneManager.sceneLoaded += Carrega;
}
#region Carrega
void Carrega(Scene cena, LoadSceneMode modo)
{
youLosePanel = GameObject.Find(“YouLose”);
denovo = GameObject.Find(“denovo”).GetComponent();
RestartLvl();
denovo.onClick.AddListener(Jogarnovamente);
//PegaDados();
#endregion
}
void PegaDados()
{
//denovo.onClick.AddListener(Jogarnovamente);
RestartLvl();
//Jogarnovamente();
}
// Start is called before the first frame update
void Start()
{
RestartLvl();
denovo = GetComponent();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.R))
{
Jogarnovamente();
}
if (gameOver && !youLosePanel.activeSelf) {
FimdeJogo();
}
}
void Jogarnovamente()
{
SceneManager.LoadScene(1);
}
void RestartLvl()
{
gameOver = false;
youLosePanel.SetActive(false);
personagem = (int)character.personagem;
}
void FimdeJogo()
{
youLosePanel.gameObject.SetActive(true);
}
/*private void OnDestroy()
{
SceneManager.sceneLoaded -= Carrega;
}*/
}
Also Sorry for my english, im fluent, but my writting is horriffic.