0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(DialogueTrigger))]
public class DialogueTriggerEditor : Editor
{
    private SerializedProperty _conversations;

    private void OnEnable()
    {
        _conversations = serializedObject.FindProperty("conversations");
    }

    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        serializedObject.Update();

        _conversations.arraySize = EditorGUILayout.IntField("Conversations Size", _conversations.arraySize);

        for (int x = 0; x < _conversations.arraySize; x++)
        {
            var conversation = _conversations.GetArrayElementAtIndex(x);

            var conversationName = conversation.FindPropertyRelative("conversationName");

            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(conversationName);

            EditorGUI.indentLevel++;
            var _dialogues = conversation.FindPropertyRelative("Dialogues");

            _dialogues.arraySize = EditorGUILayout.IntField("Dialogues size", _dialogues.arraySize);

            for (int i = 0; i < _dialogues.arraySize; i++)
            {
                var dialogue = _dialogues.GetArrayElementAtIndex(i);
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue " + i), true);

                EditorGUI.indentLevel--;
            }

            if (_dialogues.arraySize > 0)
            {
                if (GUILayout.Button("Save Conversation"))
                {

                }
            }

            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

It seems like the serializedObject contains old 3 dialogues with text names and sentences even if I set the conversations size to 0 and all dialogues also set in the inspector to 0 but still when using a break point on the line:

serializedObject.Update();

I see on the serializedObject that it contains the old dialogues. Either when running the game or in the editor I see the old dialogues. And then when running the game it's using this dialogues.

But I want that if I set the conversations size to 0 or the dialogues size to 0 to delete all dialogues sentences names all text.

Here is a screenshot showing when using a break point on it before the game is running: There are 3 dialogues names and sentences:

Dialogues

And this is a screenshot of the editor: Everything is empty the conversations size is set to 0. But still when running the game it's taking/getting the old dialogues from the serializedObject.

I'm not sure yet where this old 3 dialogues are kept. In the editor memory ?

empty

Ok after a long search I found that the text of the dialogues names and sentences is stored for some reason inside the project scene file:

Results

With my program that search inside files I could find it in the scene .unity file of my project.

Still I can't figure out what object or what script store it or call it or what and why make it show when running the game.

But at least I found where it stored. What next ? I have no clue.

After edited the file in notepad I found the area of the text:

GameObject:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  serializedVersion: 6
  m_Component:
  - component: {fileID: 1705786629}
  - component: {fileID: 1705786630}
  m_Layer: 0
  m_Name: DialogueTrigger
  m_TagString: Untagged
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!4 &1705786629
Transform:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1705786628}
  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
  m_LocalPosition: {x: 0, y: 0, z: 0}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_Children: []
  m_Father: {fileID: 104156395}
  m_RootOrder: 2
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1705786630
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1705786628}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 8a658bdd09c49324ba103199b05e0b88, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  conversations: []
  dialogue:
  - name: NAVI
    sentences:
    - Hey Hey, Someone is coming...
    - Quick let's hide.
  - name: PLAYER
    sentences:
    - I'm the player so....
    - And I can't play now
  - name: NEW MAN
    sentences:
    - Hello my friend
    - I need help
    - I lost my basket full of apples
    - Please help me find it
    - Your award will help you in your quest
  dialogueNum: 0

But it's a small part of the file not sure if it's telling anything more.

\$\endgroup\$
  • 1
    \$\begingroup\$ Looks like you have a copy of your DialogueTrigger asset saved in this scene. Try deleting that reference, and reference your original asset instead. \$\endgroup\$ – DMGregory Apr 3 at 14:42
  • \$\begingroup\$ I guess something did a copy of it. But where from should I start deleting and until where ? \$\endgroup\$ – Dubi Duboni Apr 3 at 14:44
  • \$\begingroup\$ When deleting the scene file and then in the editor saving the scene and project again it's creating the same scene file with the text inside. \$\endgroup\$ – Dubi Duboni Apr 3 at 14:49
  • 1
    \$\begingroup\$ Is this object a scene instance? If so, that's what we'd expect, no? \$\endgroup\$ – DMGregory Apr 3 at 14:57
  • 1
    \$\begingroup\$ Oh, did you use Undo.RecordObject anywhere to indicate to the engine that the object has changed and needs to be updated in the scene file? \$\endgroup\$ – DMGregory Apr 3 at 15:52
1
\$\begingroup\$

Your current code never removes items, so they stick around.

You can use ClearArray() to erase all of the items when setting the length to zero, or DeleteArrayElementAtIndex() to remove items past the new end of the array one by one:

int newSize = EditorGUILayout.IntField("Conversations Size", _conversations.arraySize); 

// Delete items from the end when scaling-down the array
for (int i = _conversations.arraySize - 1; i >= newSize; i--)
   _conversations.DeleteArrayElementAtIndex(i);

You'll also want to call Undo.RecordObject() before applying your modified properties, so the engine is aware that the GameObject you're editing has changed and needs those changes serialized into the scene file for saving / play mode. This will also let you provide clearer messaging in the undo menu to track your changes.

\$\endgroup\$
  • \$\begingroup\$ Still not working. I added your solution after the line serializedObject.Update(); and changed the line to int newSize....But still it's showing the text and dialogues when running the game even if conversations size is 0. \$\endgroup\$ – Dubi Duboni Apr 2 at 19:49
  • 1
    \$\begingroup\$ I'm unable to reproduce the issue. Testing with the code above resizes the array as expected, and the changes are reflected when I run the game. Is it possible you have a second copy of the object somewhere that your game script is reading from? \$\endgroup\$ – DMGregory Apr 3 at 0:16
  • \$\begingroup\$ No I checked few times there is no copy of it. And I searched for text files in my project on the hard disk searched for other objects in hierarchy. no that the script is editor so it's not attached to anything but the text it self I can't find where is he stored. \$\endgroup\$ – Dubi Duboni Apr 3 at 0:28
  • \$\begingroup\$ Another thing while the game is running I can see in the Dialogue System in the hierarchy where the Text textName is I see the name NAVI there. So the text is exist somewhere but I can't figure out where. Could be I did some copy to memory or somewhere to the editor somewhere inside deep that store it somehow ? If I could track to find at least where he get the text from. \$\endgroup\$ – Dubi Duboni Apr 3 at 0:30
  • \$\begingroup\$ Can you try to reproduce the problem in a new empty project with as little code as possible, to isolate what might be causing it? \$\endgroup\$ – DMGregory Apr 3 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.