Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help figuring out how to fix this bug in my script

Discussion in 'Scripting' started by Lil_Potatoe, Jul 8, 2021.

  1. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Hey
    I'm trying to create a dialogue system triggered by a ontriggerenter and exit. The problem I'm encountering is that when I exit the trigger block, the text is still being typed out by the coroutine function, which i don't want. I wanted it to stop writing the dialogue when i exit the trigger block and restart so when i enter again, the dialogue starts back at the beggining. Thank you for any help and sorry about the explanation im not very good at explaining things.



    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Dialogue : MonoBehaviour
    7. {
    8.    
    9.   public TextMeshProUGUI textDisplay;
    10.   public string[] sentences;
    11.   private int index;
    12.   public float typingSpeed;
    13.   public GameObject continueButton;
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.    private void OnTriggerEnter2D(Collider2D other)
    21.    {
    22.        if ( other.gameObject.tag == "Player" )
    23.        {
    24.       StartCoroutine(Type());
    25.        }
    26.        
    27.    }
    28.  
    29.  
    30. private void OnTriggerExit2D(Collider2D other)
    31. {
    32.    
    33.      if ( other.gameObject.tag == "Player" )
    34.        {
    35.       StopCoroutine(Type());
    36.       textDisplay.text = "";
    37.       continueButton.SetActive(false);
    38.      
    39.        }
    40.  
    41. }
    42.    
    43.  
    44.  
    45.    
    46.  
    47.     private void Update()
    48.     {
    49.        
    50.         if ( textDisplay.text == sentences[index] )
    51.         {
    52.             continueButton.SetActive(true);
    53.         }
    54.  
    55.     }
    56.  
    57.  
    58.  
    59.  
    60.   IEnumerator Type()
    61.   {
    62.  
    63.       foreach ( char letter in sentences[index].ToCharArray())
    64.       {
    65.           textDisplay.text += letter;
    66.           yield return new WaitForSeconds(typingSpeed);
    67.       }
    68.  
    69.   }
    70.  
    71.  
    72.   public void NextSentence()
    73.   {
    74.       continueButton.SetActive(false);
    75.  
    76.       if ( index < sentences.Length - 1)
    77.       {
    78.           index++;
    79.           textDisplay.text = "";
    80.           StartCoroutine(Type());
    81.       }
    82.       else
    83.       {
    84.           textDisplay.text = "";
    85.            continueButton.SetActive(false);
    86.       }
    87.  
    88.   }
    89.  
    90.  
    91. }
     
    Last edited: Jul 8, 2021
  2. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    here are some images to better explain my problem


    when you enter in the trigger block (the pink rectangle) and stay in it while the dialogue types it self out there are no problems

    Screen Shot 2021-07-08 at 11.44.13 AM.png


    But when you enter and exit quickly you start getting something like this, which does not disappear even when you exit the block.


    Screen Shot 2021-07-08 at 11.45.15 AM.png
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    Lil_Potatoe likes this.
  4. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Hey, Thanks for the help!!!
    I did take your advice and sprinkled some debug.log into my code and I have found the problem. The typing method will finish the sentence it currently is on even with the coroutine stopped because of the for each loop. All I had to do was add an if statement inside the for each loop that would only allow it to type the dialogue if the bool was true ( player entered trigger) and would stop it otherwise.
     
    Kurt-Dekker likes this.