The code is working fine but with one litte misstake. When it ends it is not closing and it is repeating the same text.All i want is it to dissapear after there are no sentences and if the player wants to see the text again to press F key again
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Dialogue : MonoBehaviour
{
public GameObject window;
public GameObject indicator;
public TMP_Text dialogueText;
public List<string> dialogues;
public float writingSpeed;
private int index;
private int charIndex;
private bool started;
private bool waitForNext;
private void Awake()
{
ToggleIndicator(false);
ToggleWindow(false);
}
public void ToggleWindow(bool show)
{
window.SetActive(show);
}
public void ToggleIndicator(bool show)
{
indicator.SetActive(show);
}
public void StartDialogue()
{
if (started)
return;
started = true;
ToggleWindow(true);
ToggleIndicator(false);
GetDialogue(0);
}
private void GetDialogue(int i)
{
index = i;
charIndex = 0;
dialogueText.text = string.Empty;
StartCoroutine(Writing());
}
public void EndDialogue()
{
started = false;
waitForNext = false;
StopAllCoroutines();
ToggleWindow(false);
}
IEnumerator Writing()
{
yield return new WaitForSeconds(writingSpeed);
string currentDialogue = dialogues[index];
dialogueText.text += currentDialogue[charIndex];
charIndex++;
if (charIndex < currentDialogue.Length)
{
yield return new WaitForSeconds(writingSpeed);
StartCoroutine(Writing());
}
else
{
waitForNext = true;
}
}
void Update()
{
if (!started)
return;
if (waitForNext && Input.GetKeyDown(KeyCode.F))
{
waitForNext = false;
index++;
if (index < dialogues.Count)
{
GetDialogue(index);
}
else
{
EndDialogue();
}
}
}
}