Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    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 Timeline Dialogue Function

Discussion in 'Timeline' started by StaciaStay, Jan 5, 2023.

  1. StaciaStay

    StaciaStay

    Joined:
    Dec 10, 2022
    Posts:
    4
    Alright so I have a pretty good dialogue system for the game I am making, however I'm having trouble with the timeline feature. Now i love timeline, it's incredible and I would like to use it more. But I need to figure out this issue I keep having.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Dialogue : MonoBehaviour
    7. {
    8.     //Fields
    9.     //Window
    10.     public GameObject window;
    11.     //Indicator
    12.     public GameObject indicator;
    13.     //Text component
    14.     public TMP_Text dialogueText;
    15.     //Dialogues list
    16.     public List<string> dialogues;
    17.     //Writing speed
    18.     public float writingSpeed;
    19.     //Index on dialogue
    20.     private int index;
    21.     //Character index
    22.     private int charIndex;
    23.     //Started boolean
    24.     private bool started;
    25.     //Wait for next boolean
    26.     private bool waitForNext;
    27.  
    28.     private void Awake()
    29.     {
    30.         ToggleIndicator(false);
    31.         ToggleWindow(false);
    32.     }
    33.  
    34.     private void ToggleWindow(bool show)
    35.     {
    36.         window.SetActive(show);
    37.     }
    38.     public void ToggleIndicator(bool show)
    39.     {
    40.         indicator.SetActive(show);
    41.     }
    42.  
    43.     //Start Dialogue
    44.     public void StartDialogue()
    45.     {
    46.         if (started)
    47.             return;
    48.  
    49.         //Boolean to indicate that we have started
    50.         started = true;
    51.         //Show the window
    52.         ToggleWindow(true);
    53.         //hide the indicator
    54.         ToggleIndicator(false);
    55.         //Start with first dialogue
    56.         GetDialogue(0);
    57.     }
    58.  
    59.     private void GetDialogue(int i)
    60.     {
    61.         //start index at zero
    62.         index = i;
    63.         //Reset the character index
    64.         charIndex = 0;
    65.         //clear the dialogue component text
    66.         dialogueText.text = string.Empty;
    67.         //Start writing
    68.         StartCoroutine(Writing());
    69.     }
    70.  
    71.     //End Dialogue
    72.     public void EndDialogue()
    73.     {
    74.         //Stared is disabled
    75.         started = false;
    76.         //Disable wait for next as well
    77.         waitForNext = false;
    78.         //Stop all Ienumerators
    79.         StopAllCoroutines();
    80.         //Hide the window
    81.         ToggleWindow(false);      
    82.     }
    83.     //Writing logic
    84.     IEnumerator Writing()
    85.     {
    86.         yield return new WaitForSeconds(writingSpeed);
    87.  
    88.         string currentDialogue = dialogues[index];
    89.         //Write the character
    90.         dialogueText.text += currentDialogue[charIndex];
    91.         //increase the character index
    92.         charIndex++;
    93.         //Make sure you have reached the end of the sentence
    94.         if(charIndex < currentDialogue.Length)
    95.         {
    96.             //Wait x seconds
    97.             yield return new WaitForSeconds(writingSpeed);
    98.             //Restart the same process
    99.             StartCoroutine(Writing());
    100.         }
    101.         else
    102.         {
    103.             //End this sentence and wait for the next one
    104.             waitForNext = true;
    105.         }      
    106.     }
    107.  
    108.     private void Update()
    109.     {
    110.         if (!started)
    111.             return;
    112.  
    113.         if(waitForNext && Input.GetKeyDown(KeyCode.E))
    114.         {
    115.             waitForNext = false;
    116.             index++;
    117.  
    118.             //Check if we are in the scope fo dialogues List
    119.             if(index < dialogues.Count)
    120.             {
    121.                 //If so fetch the next dialogue
    122.                 GetDialogue(index);
    123.             }
    124.             else
    125.             {
    126.                 //If not end the dialogue process
    127.                 ToggleIndicator(true);
    128.                 EndDialogue();
    129.             }          
    130.         }
    131.     }
    132.  
    133. }
    This my normal code for dialogues when the player is playing the game (not in a cutscene)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DialogueTrigger : MonoBehaviour
    6. {
    7.     public Dialogue dialogueScript;
    8.     private bool playerDetected;
    9.  
    10.     //Detect trigger with player
    11.     private void OnTriggerEnter2D(Collider2D collision)
    12.     {
    13.         //If we triggerd the player enable playerdeteced and show indicator
    14.         if(collision.tag == "Player")
    15.         {
    16.             playerDetected = true;
    17.             dialogueScript.ToggleIndicator(playerDetected);
    18.         }
    19.     }
    20.  
    21.     private void OnTriggerExit2D(Collider2D collision)
    22.     {
    23.         //If we lost trigger  with the player disable playerdeteced and hide indicator
    24.         if (collision.tag == "Player")
    25.         {
    26.             playerDetected = false;
    27.             dialogueScript.ToggleIndicator(playerDetected);
    28.             dialogueScript.EndDialogue();
    29.         }
    30.     }
    31.     //While detected if we interact start the dialogue
    32.     private void Update()
    33.     {
    34.         if(playerDetected && Input.GetKeyDown(KeyCode.E))
    35.         {
    36.             dialogueScript.StartDialogue();
    37.         }
    38.     }
    39. }
    I'm not sure if this is needed to be shown but this is my dialogue trigger script.

    I want my cutscenes to have the same dialogue box, with text and typewriter effects, and still have the player able to press E before continuing the cutscene.
    I have tried putting the dialogue prefab as a control track, but every time I play back in game, it disappears.

    I'm so confused idk even know how to start fixing this