Hey Guys,
I’m writing a script to control the dialogue between my player and an NPC. So far, I have been able to make a script that will display my NPC’s message, update the quest panel, and then close the dialogue box when the player clicks again. after that, the script will display a different message until the quest is completed. and it works great.
However, It only works great if you can get away with putting the entire conversation within a single chat bubble. So I decided to try and adjust my script to be able to continue the conversation through multiple bubbles. After a few hours of trying different methods, I came up with this.
var TextBox : GameObject;
var TextMessage : String;
var MessageBox : GameObject;
var QuestBox : GameObject;
var QuestText : GameObject;
var QuestName : String;
var TextBoxOnCheck : boolean;
function Update () {
talk ();
}
function OnMouseDown () {
if (TextBoxOnCheck == false) {
TextBoxOnCheck = true;
//talk ();
}
else {
Debug.Log ("else");
TextBoxOnCheck = false;
MessageBox.SetActive (false);
}
}
function talk () {
Debug.Log ("Talk");
if (TextBoxOnCheck){
MessageBox.SetActive (true);
TextMessage = TextMessage;
TextMessage = "What do you want, human.";
TextBox.GetComponent.<Text>().text = TextMessage;
while (TextBoxOnCheck){
if (Input.GetButtonDown ("Continue")){
Debug.Log ("Next.");
TextMessage = "What? Some farmer wants his bag of grain back? I don't have time to deal with something like that!";
TextBox.GetComponent.<Text>().text = TextMessage;
}
}
}
}
I figured by setting my TextBoxOnCheck from on int ( either 0 or 1) to a boolean, then I could just run the function anytime that boolean comes back as true, and I can set it as true with the OnMouseDown function.
I have tried this three different times and each time unity freezes and I have to end the task. So, logically, something about my script is crashing my game.
I’m fairly new to coding, and brand new to javescript. I have a little bit of experience in C#, but i’d still consider myself a beginner. So if you guys see my mistake, or if you know of any better ways to accomplish this, then i’m all ears!
Thanks!
Note: Forums say its in C#, but i’m using Java and don’t see a way to change that…
Update: I have narrowed the culprit down to be my while statement. anytime that line is executed, the game crashes. I have updated the script in my post to be my most recent.