Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Dialogue

Discussion in 'Scripting' started by Doesntmatterpod, Jun 15, 2020.

  1. Doesntmatterpod

    Doesntmatterpod

    Joined:
    Jun 8, 2020
    Posts:
    1
    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.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityStandardAssets.Characters.FirstPerson;
    7.  
    8. public class Inizio_001 : MonoBehaviour
    9. {
    10.  
    11.     public GameObject playerObject;
    12.     public GameObject FadeScreenIn;
    13.     public GameObject SfondoNero;
    14.     public GameObject TextBox;  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         playerObject.GetComponent<FirstPersonController>().enabled = false;
    20.         StartCoroutine (ScenePlayer());
    21.     }
    22.  
    23.     IEnumerator ScenePlayer ()
    24.     {
    25.         SfondoNero.SetActive (true);
    26.         yield return new WaitForSeconds (2f);
    27.         TextBox.GetComponent<Text> ().text = "...";
    28.         yield return new WaitForSeconds (2);
    29.         TextBox.GetComponent<Text> ().text = "La mia...";
    30.         yield return new WaitForSeconds (2);
    31.         TextBox.GetComponent<Text> ().text = "La mia testa...";
    32.         yield return new WaitForSeconds (2);
    33.         TextBox.GetComponent<Text> ().text = "Fa un male cane...";
    34.         yield return new WaitForSeconds (2);
    35.         TextBox.GetComponent<Text> ().text = "...";
    36.         yield return new WaitForSeconds (2);
    37.         TextBox.GetComponent<Text> ().text = "Dove mi trovo?";
    38.         yield return new WaitForSeconds (2);      
    39.         SfondoNero.SetActive (false);
    40.         FadeScreenIn.SetActive (true);
    41.         yield return new WaitForSeconds (2);
    42.         TextBox.GetComponent<Text> ().text = "...";
    43.         yield return new WaitForSeconds (2);
    44.         TextBox.GetComponent<Text> ().text = "Cos'è questo posto?!";
    45.         yield return new WaitForSeconds (2);  
    46.         TextBox.GetComponent<Text> ().text = "";
    47.         playerObject.GetComponent<FirstPersonController>().enabled = true;
    48.     }
    49. }
    50.  

    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!
     
  2. MatrixQ

    MatrixQ

    Joined:
    May 16, 2020
    Posts:
    87
    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:

    Code (CSharp):
    1. switch (textcount)
    2.         {
    3.         case 1:
    4.             TextBox.GetComponent<Text> ().text = "...";
    5.             break;
    6.         case 2:
    7.            TextBox.GetComponent<Text> ().text = "La mia...";
    8.             break;
    9.         }
    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.