Question by
sho123456789 · Oct 04, 2020 at 01:33 PM ·
c#scripting problem2d game
Dialogue script shows previous sentences after a while
So, I tried doing a basic dialogue script and I attached it to multiple buttons inside my game (Im a beginner by the way)... For some reason. It works for a while, but after switching to the other button and using the similar script, old values from the previous button started showing up.
here's my code.
public Button back; public Button next; public Button close; public GameObject displayImage; public List images; public GameObject displayPanel; public Text textDisplay;
public List<string> sentences = new List<string>();
private string[] s = { "Dummy"};
private int index = 0;
private int clickcount = 0;
public float TypeSpeed;
public AudioSource speak;
public AudioSource beep;
// this shows the dialogue box
public void showDialogueBox()
{
// I tried saving it to List<> first, then I tried transfering it to an array
but same result
Array.Clear(s, 0, s.Length);
s = sentences.ToArray<string>();
displayPanel.SetActive(true);
// to clear previous text from dialogue box
textDisplay.text = "";
//For typing effect
StartCoroutine(Type());
//Adding Button listeners for the dialoguebox
back.onClick.AddListener(() => backCall());
next.onClick.AddListener(() => nextCall());
close.onClick.AddListener(() => enableButtons());
}
private void backCall() { BackSentence(); }
private void nextCall() { NextSentence(); }
private void enableButtons() { index = 0; }
//Coroutine for typing effect
IEnumerator Type()
{
back.interactable = false;
close.interactable = false;
next.interactable = false;
speak.Play();
beep.Play();
int i = 0;
while( i < s[index].Length)
{
textDisplay.text = s[index].Substring(0, i);
i++;
yield return new WaitForSeconds(TypeSpeed);
}
speak.Stop();
beep.Stop();
back.interactable = true;
close.interactable = true;
next.interactable = true;
displayImage.GetComponent<Image>().sprite = this.images[this.index];
indexCheck();
checkArray();
}
//load next dialogue
public void NextSentence()
{
if (index < s.Length - 1)
{
++index;
textDisplay.text = "";
StartCoroutine(Type());
}
else
{
return;
}
textDisplay.text = "";
}
//return previous dialogue
public void BackSentence()
{
if (index > 0)
{
--index;
textDisplay.text = "";
StartCoroutine(Type());
}
else
{
return;
}
textDisplay.text = "";
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613013059im_/https://answers.unity.com/themes/thub/images/avi.jpg)