Dialogue

Goodmorning everyone,
I state that I am absolutely new to programming languages (who knows how many discussions start this way), so I am truly ignorant on the subject.
I am following tutorials to “create a video game” for personal hobby, but for two days I have been going crazy behind a script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

public class Inizio_001 : MonoBehaviour
{
  
    public GameObject playerObject;
    public GameObject FadeScreenIn;
    public GameObject SfondoNero;
    public GameObject TextBox;  
  
    // Start is called before the first frame update
    void Start()
    {
        playerObject.GetComponent<FirstPersonController>().enabled = false;
        StartCoroutine (ScenePlayer());
    }
  
    IEnumerator ScenePlayer ()
    {
        SfondoNero.SetActive (true);
        yield return new WaitForSeconds (2f);
        TextBox.GetComponent<Text> ().text = "...";
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "La mia...";
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "La mia testa...";
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "Fa un male cane...";
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "...";
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "Dove mi trovo?";
        yield return new WaitForSeconds (2);      
        SfondoNero.SetActive (false);
        FadeScreenIn.SetActive (true);
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "...";
        yield return new WaitForSeconds (2);
        TextBox.GetComponent<Text> ().text = "Cos'è questo posto?!";
        yield return new WaitForSeconds (2);  
        TextBox.GetComponent<Text> ().text = "";
        playerObject.GetComponent<FirstPersonController>().enabled = true;
    }
}

Practically: the game start on a black background and on this background, texts appear. I wish these texts could proceed by pressing a button on the keyboard and automatically with the “WaitForSeconds” command as I did (to fall back on the problem).

How can I solve this?

Thanks so much!

You’re going to want to separate the input from the output, to make it work better.

Have one method that contains all the text as a switch case connected to an int. Like so:

switch (textcount)
        {
        case 1:
            TextBox.GetComponent<Text> ().text = "...";
            break;
        case 2:
           TextBox.GetComponent<Text> ().text = "La mia...";
            break;
        }

and so on.

Now you make another method that counts up. You’re going to want to make this a coroutine and run it every frame, so you yield return null. In that method, you do 2 things.

  1. Count up the time. Get a start value for your time With Time.time and check if 2 seconds have passed. If so, increase your textcount and run the other method to set the new text with that new value.
  2. Catch any input. On an input, reset the starttime, increase your textcount and run the output method with the new value.

Hope that gives you a bit of direction.