Best way to script a conversation system

So right now I have a pretty basic script for a conversation, problem is, if you click on an answer, I’m not sure how to redirect to different conversation lines other than simply redirecting to another script (by enabling another script and loading it on the object), which seems a bit superfluous.

Here is my script example… right now after you click on a button it simply prints a comment in the console.

using UnityEngine;
using System.Collections;

public class Conversation : MonoBehaviour {
  
  
    void OnGUI() {
        GUIStyle style = new GUIStyle ();
        style.richText = true;

        GUI.Box(new Rect(0, 0, 400, 200), "");
        GUILayout.Label("<size=30><color=white>My greetings to you, earthling</color></size>",style);
        if (GUI.Button(new Rect(10, 50, 100, 50), "My greetings to you as well"))
            print("You clicked the button!");
        if (GUI.Button(new Rect(10, 100, 100, 50), "Earthling? What do you mean, earthling?"))
            print("You clicked the button!");
    }
}

There have been some pretty good discussions about conversation systems in previous forum threads such as this one.

One of the main takeaways is to keep data and code separate.

This encourages you to make general-purpose code rather than scripts containing hard-coded content. By separating data and code, you’ll probably end up with a single script takes a data file as input and “plays” it as a conversation – showing conversation lines, presenting buttons to the player, and advancing the conversation when the player clicks a button.

(There are also products on the Asset Store if you want to bypass the coding and get right to writing content.)

1 Like