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 >_<
I use 4 scripts for this.
1)
```csharp
**using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace InteractiveSystem
{
public class InteractiveBaseClass : MonoBehaviour
{
public bool finished {get; protected set;}
protected IEnumerator WriteText(string input, Text textHolder, Color textColor, Font textFont, float delay, float delayBetweenLines)
{
textHolder.color = textColor;
textHolder.font = textFont;
for (int i = 0; i < input.Length; i++)
{
textHolder.text += input [i];
yield return new WaitForSeconds(delay);
}
yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.E));
finished = true;
}
}
}**
** **2)** **
csharp
**using System.Collections;
using UnityEngine;
namespace InteractiveSystem
{
public class InteractiveHolder : MonoBehaviour
{
private void OnEnable()
{
StartCoroutine(dialogueSequence());
}
private IEnumerator dialogueSequence()
{
for(int i = 0; i < transform.childCount; i++)
{
Deactivate();
transform.GetChild(i).gameObject.SetActive(true);
yield return new WaitUntil(()=> transform.GetChild(i).GetComponent<InteractiveLines>().finished);
}
gameObject.SetActive(false);
}
private void Deactivate()
{
for (int i=0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
}
}
}**
** **3)** **
csharp
**using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace InteractiveSystem
{
public class InteractiveLines : InteractiveBaseClass
{
private Text textHolder;
[Header("Text options")]
[SerializeField] private string input;
[SerializeField] private Color textColor;
[SerializeField] private Font textFont;
[Header("Time parameters")]
[SerializeField] private float delay;
[SerializeField] private float delayBetweenLines;
private IEnumerator lineAppear;
// private void Awake()
//{
//}
private void OnEnable()
{
ResetLine();
lineAppear = WriteText(input, textHolder, textColor, textFont, delay, delayBetweenLines);
StartCoroutine(lineAppear);
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
if(textHolder.text != input)
{
StopCoroutine(lineAppear);
textHolder.text = input;
}
else
finished = true;
}
}
private void ResetLine()
{
textHolder = GetComponent<Text>();
finished = false;
}
}
}**
** **4)** **
csharp
**using UnityEngine;
public class NPC_Controller : MonoBehaviour
{
[SerializeField] private GameObject dialogue;
public void ActivateDialogue()
{
dialogue.SetActive(true);
}
public bool DialogueActive()
{
return dialogue.activeInHierarchy;
}
}**
```