Dialogue Script Crashes Unity!

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.

You are causing the game to get stuck.

You are calling Talk() from update, and then running an infinite loop inside the while().

As soon as you enter Update() with TextBoxOnCheck → Talk is called → while() enters and never exits. Unity cannot continue processing until Update() finishes, which it never will, since it is stuck in the while

@skalev that makes sense. the reason I put the talk function in the update is because it was the only way I could think of to keep the game to keep “tabs” on whether or not the player hits the input key. other wise when the player clicks, unless the player hits the button at the same time, it doesn’t register it. which kind of defeats the purpose of having a press to continue key.

So how do I make unity check to see if the player has pressed the button or not?

Update is called once a frame so it is a loop already. replace your while with an if, and i think you’ll get the result you wanted.

@skalev replacing the while with an if statement does fix the problem of Unity crashing, but it only display the new message for a split second before reverting it back to my original message.

I had a quick skim through and is that not because you are using OnMouseDown and instead want to use
https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html

@TaleOf4Gamers_1 does that require the player to hold the mouse button? or just whether or not its been clicked?