The problem is that the text is not allowing me to exit after it has exceeded the index for some reason, and is adding the text at its original index. I’m very confused and have done all I can to try and fix it. Any help is very appreciated.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour {
public Text textdisplay;
public Text nametext;
public string[] sentences;
public string[] names;
private int index;
private int end = 2;
public float typingspeed;
public GameObject uimanager;
public static bool displayText = false;
private bool isActive = false;
private string trig;
public GameObject continueButton;
public GameObject script;
void Start () {
}
private void Update()
{
trig = Clicker.trigname;
NameTxt();
Conversations();
if (displayText == true)
{
uimanager.SetActive(true);
}
else
{
uimanager.SetActive(false);
}
if (textdisplay.text == sentences[index])
{
continueButton.SetActive(true);
}
}
IEnumerator Type()
{
foreach (char letter in sentences[index].ToCharArray())
{
if(index <= end)
{
textdisplay.text += letter;
yield return new WaitForSeconds(typingspeed);
}
else
{
isActive = false;
displayText = false;
}
}
}
public void NextSentence()
{
continueButton.SetActive(false);
if (index < sentences.Length - 1 && index < end) {
index++;
textdisplay.text = "";
StartCoroutine(Type());
}
else
{
isActive = false;
displayText = false;
}
}
void NameTxt()
{
nametext.text = names[index];
}
//Problem: GOES BACK TO THE FIRST SENTENCE? THEN STOPS ALLOWING YOU TO EXIT.
void Conversations()
{
Conv1();
Conv2();
}
void Conv1()
{
if(trig == "conv1" && isActive == false)
{
index = 0;
end = 2;
isActive = true;
displayText = true;
StartCoroutine(Type());
}
}
void Conv2()
{
if (trig == "conv2" && isActive == false)
{
index = 3;
end = 5;
isActive = true;
displayText = true;
StartCoroutine(Type());
}
}
}