-1

If I click on the "Save Conversations" or the "Load Conversations" button the variable _conversationTrigger is null.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(ConversationTrigger))]
public class ConversationTriggerEditor : Editor
{
    private ConversationTrigger _conversationTrigger;

    [SerializeField] private ReorderableList conversationsList;

    private SerializedProperty _conversations;

    private int _currentlySelectedConversationIndex = -1;
    private int newSize = 0;
    private Vector2 scrollPos;

    private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();


    public override void OnInspectorGUI()
    {
        if (GUILayout.Button("Edit Conversations"))
        {
            ConversationsEditorWindow myWindow = CreateInstance<ConversationsEditorWindow>();

            myWindow.minSize = new Vector2(1500, 900);
            myWindow.maxSize = new Vector2(1500, 900);
            myWindow.Init(serializedObject);

        }

        if (GUILayout.Button("Save Conversations"))
        {
            _conversationTrigger.SaveConversations();
        }

        if (GUILayout.Button("Load Conversations"))
        {
            Undo.RecordObject(_conversationTrigger, "Loaded conversations from JSON");
            _conversationTrigger.LoadConversations();
        }
    }
}

And the top of the ConversationsEdiotrWindow script the script is much longer. This is where I'm using the Init method to init the _conversationTrigger :

When using break point on the _conversationTrigger in the ConversationsEdiotrWindow it's fine but in the ConversationTriggerEditor script it's null.

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

public class ConversationsEditorWindow : EditorWindow
{
    public SerializedObject conversation = null;

    private ConversationTrigger _conversationTrigger;

    [SerializeField] private ReorderableList conversationsList;

    private SerializedProperty _conversations;

    private int _currentlySelectedConversationIndex = -1;
    private int newSize = 0;
    private Vector2 scrollPos;

    private readonly Dictionary<string, ReorderableList> _dialoguesListDict = new Dictionary<string, ReorderableList>();
    private readonly Dictionary<string, ReorderableList> _sentencesListDict = new Dictionary<string, ReorderableList>();

    private SerializedObject itemcopy;
    private string searchString = "";

    public void Init(SerializedObject _item)
    {
        // Copy the Item targetObject to not lose reference when you
        // click another element on the project window.
        itemcopy = new SerializedObject(_item.targetObject);
        conversation = itemcopy;

        // Other things to initialize the window

        const int width = 1500;
        const int height = 900;

        var x = (Screen.currentResolution.width - width) / 2;
        var y = (Screen.currentResolution.height - height) / 2;

        var window = GetWindow<ConversationsEditorWindow>();
        window.position = new Rect(x, y, width, height);

        _conversationTrigger = (ConversationTrigger)_item.targetObject;
        _conversations = itemcopy.FindProperty("conversations");
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120

1 Answers1

1

You have two different objects of type ConverstationTrigger in two different places. One is instantiated in ConverstationsEditorWindow while the other is null because there is no assignment in ConversationTriggerEditor.

By the looks of it, you want to use only one ConverstationTrigger. Perhaps there is a solution where you can inject the instance of ConverstationTrigger into the places you need it via the a constructor of some sort.

Jimenemex
  • 3,104
  • 3
  • 24
  • 56