2

Not what is the exception but why I'm getting it ?

MissingReferenceException: The object of type 'PlayConversations' 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.

This is a screenshot before running the game. The script PlayConversations is only attached to the object Conversation Trigger :

Before running the game

After running the game at the point in the game it's trying to use the PlayConversations script I'm getting the exception. And I looked many times I can't find any object destroyed or that PlayConversations is not attached or destroyed or missing.

This is a screenshot after the game is running it's creating a Clone of the Player and I can still see the PlayConversations script :

After running the game

This is the script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayConversations : MonoBehaviour
{
    private static ConversationTrigger conversationTrigger;
    private static PlayConversations instance;

    private void Awake()
    {
        conversationTrigger = GetComponent<ConversationTrigger>();
        instance = this;
    }

    public static void AddConversationToPlay(int index)
    {
        ConversationTrigger.conversationsToPlay.Add(index);
    }

    public static void PlayMultipleConversations()
    {
        instance.StartCoroutine(conversationTrigger.PlayConversations());
    }

    public static void PlaySingleConversation(int ConversationIndex)
    {
        instance.StartCoroutine(conversationTrigger.PlayConversation(ConversationIndex));
    }  
}

The exception happens on the line :

instance.StartCoroutine(conversationTrigger.PlayConversation(ConversationIndex));

It seems like instance is null.

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • You should throw an exception on Awake in case `instance` is not null, because your design only allow one instance. That said, you probably shouldn't create static classes like that... you can see how this can go wrong by having multiple instances. of this class. Take a look at [this answer](https://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp) and think if your class should really be static. Add a log to `OnDestroy()` so you can debug why your instance is going to null. – Roberto Feb 12 '20 at 02:20
  • Also, you shouldn't name your class PlayConversations, class names should be nouns, not actions/verbs, so ConversationPlayer, like you did with the trigger :) – Roberto Feb 12 '20 at 02:21

1 Answers1

1

It looks like you are using a static variable for the conversationTrigger which you set on Awake for the instance.

Is it possible that you have multiple instances of this component in the game and Awake is assigning null to the variable in a different instance.

I would suggest adding some logging in your Awake call and log some information in the assignment there. If that is the problem, you should be able to click the log and jump to the object in your scene.

Ian Pilipski
  • 507
  • 3
  • 8