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