When changing scenes, the Script that contains an Invoke() gives an error

29 views Asked by At

I have an Invoke() inside a Coroutine. This Invoke() looks like this:

private IEnumerator aguardarResultado() 
{
    while(!_rigidbody.IsSleeping()) 
    {
        yield return null;
    }
    int numSorteado = 1;
    numSorteado = Vector3.Dot(Vector3.up, -transform.up) > 0.6f ? 6 : numSorteado;
    numSorteado = Vector3.Dot(Vector3.up, transform.forward) > 0.6f ? 3 : numSorteado;
    numSorteado = Vector3.Dot(Vector3.up, -transform.forward) > 0.6f ? 4 : numSorteado;
    numSorteado = Vector3.Dot(Vector3.up, transform.right) > 0.6f ? 5 : numSorteado;
    numSorteado = Vector3.Dot(Vector3.up, -transform.right) > 0.6f ? 2 : numSorteado;

    OnNumSorteado?.Invoke(numSorteado);

    horizontalLayoutGroup.enabled = true;
    transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
}

"OnNumSorteado" is a public static event System.Action<int> OnNumSorteado; declared in the attributes of the Dado3D Script.

The error is happening in the line OnNumSorteado?.Invoke(numSorteado);

When I open the game scene for the first time, it runs without problems. But when I open this same scene from the 2nd time onwards, it gives an error. What is after this line OnNumSorteado?.Invoke(numSorteado); is not executed. The error is this:

MissingReferenceException: The object of type 'Image' 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.
Ludo3DPowers.recebeValorDoDado3D (System.Int32 numSorteado) (at Assets/SCRIPTS/[Ludo3DPowers_e_Poderes]/Ludo3DPowers/Ludo3DPowers.cs:890)
Dado3D+<awaitResult>d__58.MoveNext () (at Assets/SCRIPTS/Dado3D_ItemDadoNoCanvas/Dado3D.cs:295)
UnityEngine.SetupCoroutine.InvokeMoveNext(System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <685c48cf8f0b48abb797275c046dda6a>:0)

The problem is not in the 'MissingReferenceException' of 'Image'. It is a consequence of the Invoke() error that was not performed correctly. What's going on with Invoke? What do I do to fix this error?

I couldn't solve the problem

0

There are 0 answers