Continue dialogue with button press?

I’ve searched so far and wide and just feel like a fool for not finding it.
How do you get dialogue to get triggered and able to go through said dialogue with pressing of an key? I’m new, but this little thing is all I need for my super super basic demo to work correctly

Didn’t do it before but maybe you can assign parts of dialogue to a certain value of a variable.

If it is a text, you can do something like this:

public float ThisTextValue;

public void TextSetActive(float currentTextValue)
{
    if (currentTextValue == ThisTextValue)
    {
        gameObject.SetActive(true);
    }
    else
    {
        gameObject.SetActive(false);
    }
}

This script have to be assigned to all the texts. You have to choose a number for each text dialogue. 1 for the first text, 2 for the second text,…

In an empty GameObject use this script:

private float Value;
public GameObject Text; // assign any text with the script I've made before attached

void Start()
{
    Value = 0f;
}

public void AddNumber()
{
    Value += 1f; // Add 1 to the value
    ChangeValue Script = Text.GetComponent<ChangeValue>(); // Get the script of the text
    Script.TextSetActive(Value); // Use the public method made in the last Script
}

With this script, assign to the OnClick event of a button the empty GameObject and select the AddNumber Method.

You can change the different texts by different audio sources and other gameObjects.

And that should do it!

Well, for dialogs I would always use coroutines to actually go through the conversation. It makes everything much simpler and more linear from the code point of view.

To wait for a key press inside a coroutine you can simply do this:

// inside a coroutine
while (!Input.GetKeyDown(KeyCode.YourKey))
    yield return null;
// the dialog continues here

Here the coroutine is stuck inside the while loop until the desired key is pressed. If you want to use actual UI buttons and not keyboard keys, you can use my custom yield instruction “WaitForUIButtons”

Combining the two would also be possible, though a bit more work. First we could turn the first case with the keys into a custom yield instruction

public class WaitForKeyDown : CustomYieldInstruction
{
    public List<KeyCode> keys;
    public override bool keepWaiting => !ShouldContinue();
    public WaitForKeyDown(params KeyCode[] aKeys)
    {
        keys = new List<KeyCode>(aKeys);
    }
    private bool ShouldContinue()
    {
        foreach(var keyCode in keys)
        {
            if (Input.GetKeyDown(keyCode))
                return true;
        }
        return false;
    }
}

This custom yield instruction can be simply used like this:

yield return new WaitForKeyDown(KeyCode.A, KeyCode.B);

This would wait until either key “A” or key “B” has been pressed down. To combine several of these “conditions” we can create “logic gates” like AND and OR

public class CombineOR : CustomYieldInstruction
{
    public List<CustomYieldInstruction> operands;
    public override bool keepWaiting => !ShouldContinue();
    public WaitForKeyDown(params CustomYieldInstruction[] aOperands)
    {
        operands = new List<CustomYieldInstruction>(aOperands);
    }
    private bool ShouldContinue()
    {
        foreach(var operand in operands)
        {
            if (!operand.keepWaiting)
                return true;
        }
        return false;
    }
}

Now we could simply wait for either a key press or a UI button press at the same time

yield return new CombineOR(new WaitForKeyDown(KeyCode.A, KeyCode.B), waitForUIButtons);

You can go crazy with utility yield instructions. Though you should keep in mind to keep it logical and simple to understand. A proper dialog system would probably take the actual dialog data / tree from an external source like a json file. However that’s of course not necessary. Hardcoding the actual dialog gives more flexibility about what should happen at each step.