1
resposta

[Bug] Erro NullReferenceException: Object reference not set to an instance of an object

Descrição do erro:

NullReferenceException: Object reference not set to an instance of an object
DiretorMultiplayer.AvisarQueAlguemMorreu (UnityEngine.Camera camera) (at Assets/Scripts/DiretorMultiplayer.cs:24)
UnityEngine.Events.InvokableCall`1[T1].Invoke (T1 args0) (at <c6b52566f59b49fc861a7812a1ea2f6b>:0)
UnityEngine.Events.CachedInvokableCall`1[T].Invoke (System.Object[] args) (at <c6b52566f59b49fc861a7812a1ea2f6b>:0)
UnityEngine.Events.UnityEvent.Invoke () (at <c6b52566f59b49fc861a7812a1ea2f6b>:0)
Aviao.OnCollisionEnter2D (UnityEngine.Collision2D colisao) (at Assets/Scripts/Aviao.cs:50)

Ele acontece quando tento chamar interfaceInativo dentro do método AvisarQueAlguemMorreu(Camera camera) do script DiretorMultiplayer

Códigos: InterfaceDoCanvasInativo

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InterfaceDoCanvasInativo : MonoBehaviour
{
    [SerializeField] private GameObject fundo;
    [SerializeField] private Text texto;
    private Canvas canvas;

    private void Awake(){
        this.canvas = this.GetComponent<Canvas>();
    }

    public void Mostrar(Camera camera){
        this.fundo.SetActive(true);
        this.canvas.worldCamera = camera;
    }

    public void AtualizarTexto(int pontosParaReviver){
        this.texto.text = pontosParaReviver.ToString();
    }
}

DiretorMultiplayer

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiretorMultiplayer : Diretor
{
    private bool alguemMorto;
    private int pontosDesdeAMorte;
    private Jogador[] jogadores;
    private InterfaceDoCanvasInativo interfaceInativo;
    [SerializeField] private int pontosParaReviver;

    protected override void Start(){
        base.Start();
        this.jogadores = GameObject.FindObjectsOfType<Jogador>();
        this.interfaceInativo = GameObject.FindObjectOfType<InterfaceDoCanvasInativo>();
    }


    public void AvisarQueAlguemMorreu(Camera camera){
        this.alguemMorto = true;
        this.pontosDesdeAMorte = 0;
        this.interfaceInativo.AtualizarTexto(this.pontosParaReviver);
        this.interfaceInativo.Mostrar(camera);
    }

    public void ReviverSePrecisar(){
        if(alguemMorto){
            this.pontosDesdeAMorte++;
            this.interfaceInativo.AtualizarTexto(this.pontosParaReviver - this.pontosDesdeAMorte);
            if(this.pontosDesdeAMorte >= this.pontosParaReviver){
                this.ReviverJogadores();
            }
        }
    }

    public void ReviverJogadores(){
        this.alguemMorto = false;
        foreach(var jogador in this.jogadores){
            jogador.Ativar();
        }
    }
}
1 resposta

Mano, o erro se trata de uma excecao, que ela é lancada basicamente quando existe algum objeto com referencia nula, ex: Player jogador1 = null; O erro traduzido basicamente fala que alguma instancia do objeto está nula, nao foi definida. A partir dai acho que voce consegue analisar seu próprio código e ver onde está o erro, mas pelo que eu entendi do erro pode estar relacionado a DiretorMultiplayer.AvisarQueAlguemMorreu, talvez relacionado a interfaceInativo

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software