NullReferenceException: Object reference not set to an instance of an object

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.

You should use code tags in the future when posting code to make it easier to read.

It looks like GameObject.Find(“denovo”) does not find a GameObject with such a name in the scene and returns null. Then when you try to call GetComponent() on something that does not exist, you get a NullReferenceException.

Finding GameObjects using GameObject.Find is considered bad practice, and it very prone to errors (as you are just finding out). It is a better idea to add the [SerializeField] attribute to the denovo field and then assign the reference using the inspector if it is possible in your case.

If this is not possible, you can create a unique component for your “denovo” button, let’s say DenovoButton, and then you can use FindObjectOfType() to find the reference.

1 Like

Thanks my man, i notice after i posted, it was my first thread, im progammng in unity for a short time, like a month, im use to android studio and Xcoder, but thanks, you helped me a lot, even if i try and doesnt word i appreciate man!!

1 Like

It really worked, but now im having another error after i reload the level.

its says

MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

and now it takes me to this line of code

youLosePanel.SetActive(false);

and also when i restart the game objects that i istanciated in inspector are dissapearing, probably because the game are destroying it, but i dont know why they are getting destroyed

They’re getting destroyed because you reloaded the level (which destroys all the objects).