Search Unity

Question Dialogue System Issues

Discussion in 'Scripting' started by FOZZY_MORTRED, Sep 20, 2022.

  1. FOZZY_MORTRED

    FOZZY_MORTRED

    Joined:
    Apr 10, 2022
    Posts:
    3
    Hello, i have a problem with dialogue system. On the first interaction everything is ok, but when I click on the NPC again, the text is duplicated in the text box and to close it you need to click twice. You can see it in the GIF. Why is this happening? Please, help me, i'm totally noob >_<

    ezgif.com-gif-maker.gif

    I use 4 scripts for this.

    1)
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. namespace InteractiveSystem
    6. {
    7.     public class InteractiveBaseClass : MonoBehaviour
    8.     {
    9.         public bool finished {get; protected set;}
    10.  
    11.         protected IEnumerator WriteText(string input, Text textHolder, Color textColor, Font textFont, float delay, float delayBetweenLines)
    12.         {
    13.             textHolder.color = textColor;
    14.             textHolder.font = textFont;
    15.  
    16.             for (int i = 0; i < input.Length; i++)
    17.             {
    18.                 textHolder.text += input [i];
    19.                 yield return new WaitForSeconds(delay);
    20.             }
    21.         yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.E));
    22.         finished = true;
    23.         }
    24.     }
    25. }
    2)
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. namespace InteractiveSystem
    5. {
    6.     public class InteractiveHolder : MonoBehaviour
    7.     {
    8.         private void OnEnable()
    9.         {
    10.             StartCoroutine(dialogueSequence());
    11.         }
    12.  
    13.         private IEnumerator dialogueSequence()
    14.         {
    15.                 for(int i = 0; i < transform.childCount; i++)
    16.                 {
    17.                    Deactivate();
    18.                    transform.GetChild(i).gameObject.SetActive(true);
    19.                    yield return new WaitUntil(()=> transform.GetChild(i).GetComponent<InteractiveLines>().finished);
    20.                 }
    21.             gameObject.SetActive(false);
    22.         }
    23.  
    24.         private void Deactivate()
    25.         {
    26.             for (int i=0; i < transform.childCount; i++)
    27.             {
    28.                 transform.GetChild(i).gameObject.SetActive(false);
    29.             }
    30.         }
    31.     }
    32. }
    3)
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. namespace InteractiveSystem
    6. {
    7.     public class InteractiveLines : InteractiveBaseClass
    8.    {
    9.     private Text textHolder;
    10.  
    11.     [Header("Text options")]
    12.     [SerializeField] private string input;
    13.     [SerializeField] private Color textColor;
    14.     [SerializeField] private Font textFont;
    15.  
    16.     [Header("Time parameters")]
    17.     [SerializeField] private float delay;
    18.     [SerializeField] private float delayBetweenLines;
    19.  
    20.  
    21.     private IEnumerator lineAppear;
    22.  
    23.    // private void Awake()
    24.     //{
    25.      
    26.     //}
    27.  
    28.     private void OnEnable()
    29.     {
    30.         ResetLine();
    31.         lineAppear = WriteText(input, textHolder, textColor, textFont, delay, delayBetweenLines);
    32.         StartCoroutine(lineAppear);
    33.     }
    34.  
    35.     private void Update()
    36.     {
    37.         if(Input.GetKeyDown(KeyCode.E))
    38.         {
    39.             if(textHolder.text != input)
    40.         {
    41.             StopCoroutine(lineAppear);
    42.             textHolder.text = input;
    43.         }
    44.         else
    45.         finished = true;
    46.         }
    47.     }
    48.  
    49.       private void ResetLine()
    50.       {
    51.         textHolder = GetComponent<Text>();
    52.         finished = false;
    53.       }
    54.    }
    55. }
    4)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NPC_Controller : MonoBehaviour
    4. {
    5. [SerializeField] private GameObject dialogue;
    6.  
    7. public void ActivateDialogue()
    8. {
    9.     dialogue.SetActive(true);
    10. }
    11.  
    12. public bool DialogueActive()
    13. {
    14.     return dialogue.activeInHierarchy;
    15. }
    16. }
    17.  
     
    Last edited: Sep 20, 2022
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Code (CSharp):
    1. textHolder.text += input [i];
    This is probably the source of your error of double text. It's just adding on. Make sure this text is getting cleared.
     
  3. FOZZY_MORTRED

    FOZZY_MORTRED

    Joined:
    Apr 10, 2022
    Posts:
    3
    Yes, it's help with double text, but now i have another issue: when i click on NPC, an empty text block appears first, and with 2nd click appears my text. What do you think could be the reason of this? And with a line of code if possible.

    ezgif.com-gif-maker (1).gif
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You'll probably need to look at what triggers your WriteText coroutine. Since it also doesn't appear to be doing the typing affect, it's possible you're clearing the text after it gets populated as well.

    I'm not familiar with dialogue system's code or setup.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    So you know, Please don't cross-post on these forums.

    Thanks.