Custom Editor not saving data in Prefabs

Hey everyone!

I’ve recently tried my hand at the CustomEditor part of Unity and I’ve run into a problem.
When creating a prefab of my NPC it all looks fine and dandy, even editing the NPC’s Greeting texts and the responses the player can give works fine right until I start testing. It then resets back to the Prefab Basics; 0 Greetings and 0 Player-Responses.

This is my Custom Editor for now:

using UnityEditor;

[CustomEditor(typeof(NPC_Script))] 
public class NPC_Editor : Editor {
	bool greetingsUnfolded = false;
	bool choicesUnfolded = false;
	bool[] responsesUnfolded;
	
	int responseMax = 4;
	
    public override void OnInspectorGUI() {
		NPC_Script NPC = (NPC_Script) target;
		
		//Edit NPC Name
		NPC.npcName = EditorGUILayout.TextField("NPC Name ", NPC.npcName);
		
		//Edit greeting texts
		greetingsUnfolded = EditorGUILayout.Foldout(greetingsUnfolded, "Greetings");
		if(greetingsUnfolded){
			NPC.greetingAmount = EditorGUILayout.IntField("Amount ", NPC.greetingAmount);
			if(NPC.dialogueLines.Length != NPC.greetingAmount)
				NPC.dialogueLines = new string[NPC.greetingAmount];
			
			//Edit the greetings
			for(int i = 0; i < NPC.greetingAmount; i++){
				NPC.dialogueLines[i] = EditorGUILayout.TextField("", NPC.dialogueLines[i]);
			}
		}
		
		//Edit greeting texts
		choicesUnfolded = EditorGUILayout.Foldout(choicesUnfolded, "Responses ");
		if(choicesUnfolded){
			NPC.choices = EditorGUILayout.IntField("Amount ", NPC.choices);
			divLine();
			if(NPC.choices > responseMax){
				NPC.choices = responseMax;
			}
			
			if(NPC.choiceLines.Length != NPC.choices){
				NPC.choiceLines = new string[NPC.choices];
				NPC.responses = new string[NPC.choices];
				responsesUnfolded = new bool[NPC.choices];
				
			}
			
			//Edit the greetings
			for(int i = 0; i < NPC.choices; i++){
				//responsesUnfolded[i] = EditorGUILayout.Foldout(responsesUnfolded[i], NPC.choiceLines[i]);
				//if(responsesUnfolded[i]){
					NPC.choiceLines[i] = EditorGUILayout.TextField("Player Response ", NPC.choiceLines[i]);
					NPC.responses[i] = EditorGUILayout.TextField("NPC Response ", NPC.responses[i]);
					choicesUnfolded = EditorGUILayout.Foldout(choicesUnfolded, "Responses ");
					divLine();
				//}
			}
		}
    }
	
	void divLine(){
		EditorGUILayout.LabelField("_________________________________________________________");
	}
}

When I however place an NPC prefab without this script attached to the scene and THEN add this script, everything works like it should; Remembering the NPC name, the greetings and responses and everything shows up perfectly in the game-view when talking to an NPC.

Since I had no idea what I should be looking for I thought I’d post here.

I’m also trying to make a conversation tree. Should I make a recursive function or would you reccomend some other method of creating a basically infinite way of speaking with an NPC like;
Greeting, response, answer, response, answer, etc.

I hope someone can help me out.

-Zubaja

Down near the bottom of your OnInspectorGUI() method you should add:

if (GUI.changed) { EditorUtility.SetDirty(NPC); }

As far as conversation trees go, I’m going to suggest that you keep it simple. Consider a block of text (What an NPC says) as a node in a graph. The text-node can have multiple question-nodes (what the PC asks). Each question-node links to exactly one text-node. Each node just needs a unique index, and can reference other nodes via their index. Store it all in a big list and you’re good to go. This is how the infinity engine games did their dialogues.

2 Likes

Thanks a bunch Kru!

However, my code did not recognise the ‘GUI’ component, but it seems it recognises ‘EditorUtility.SetDirty()’ just fine with the ‘if’ statement.
And I’ll look into that technique for NPC conversations you mentioned.

Consider this solved.

Your solution worked for me. Saved my days. Thanks a ton. These error still occur in today’s Unity version, and yours is an accurate solution to resolve it.

To fix the “not recognised ‘GUI’ component” you have to add the using UnityEngine :wink: