Let me briefly point out what i’m working on…
1.it’s a dialog system that would run in background without the player interaction.
2.player can view the dialog when he interacts with the NPC having this script
But when i add the script on multiple NPCthe sentences of dialog overlaps with other npcs…For eg. if NPC1 has the script with its dialogues and NPC2 has the same script with different dialogues…I found out that the script glitches and mixes up the dialogues…
Could anyone help me…thanks in advance,
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class DialogueSystem : MonoBehaviour
{
public Text nameText;
public Text dialogueText;
public GameObject dialogueGUI;
public Transform dialogueBoxGUI;
public float letterDelay = 0.1f;
public float letterMultiplier = 0.5f;
public KeyCode DialogueInput = KeyCode.F;
public string Names;
public string[] dialogueLines;
public bool letterIsMultiplied = false;
public bool dialogueActive = false;
public bool dialogueEnded = false;
public bool outOfRange = true;
public AudioClip audioClip;
AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
dialogueText.text = "";
}
void Update()
{
}
public void EnterRangeOfNPC()
{
outOfRange = false;
dialogueGUI.SetActive(true);
if (dialogueActive == true)
{
dialogueGUI.SetActive(false);
}
}
public void NPCName()
{
outOfRange = false;
dialogueBoxGUI.gameObject.SetActive(true);
nameText.text = Names;
if (Input.GetKeyDown(KeyCode.F))
{
if (!dialogueActive)
{
dialogueActive = true;
StartCoroutine(StartDialogue());
}
}
StartDialogue();
}
private IEnumerator StartDialogue()
{
if (outOfRange == false)
{
int dialogueLength = dialogueLines.Length;
int currentDialogueIndex = 0;
while (currentDialogueIndex < dialogueLength || !letterIsMultiplied)
{
if (!letterIsMultiplied)
{
letterIsMultiplied = true;
StartCoroutine(DisplayString(dialogueLines[currentDialogueIndex++]));
if (currentDialogueIndex >= dialogueLength)
{
dialogueEnded = true;
}
}
yield return 0;
}
while (true)
{
if (Input.GetKeyDown(DialogueInput) && dialogueEnded == false)
{
break;
}
yield return 0;
}
dialogueEnded = false;
dialogueActive = false;
DropDialogue();
}
}
private IEnumerator DisplayString(string stringToDisplay)
{
if (outOfRange == false)
{
int stringLength = stringToDisplay.Length;
int currentCharacterIndex = 0;
dialogueText.text = "";
while (currentCharacterIndex < stringLength)
{
dialogueText.text += stringToDisplay[currentCharacterIndex];
currentCharacterIndex++;
if (currentCharacterIndex < stringLength)
{
if (Input.GetKey(DialogueInput))
{
yield return new WaitForSeconds(letterDelay * letterMultiplier);
if (audioClip) audioSource.PlayOneShot(audioClip, 0.5F);
}
else
{
yield return new WaitForSeconds(letterDelay);
if (audioClip) audioSource.PlayOneShot(audioClip, 0.5F);
}
}
else
{
dialogueEnded = false;
break;
}
}
while (true)
{
if (Input.GetKeyDown(DialogueInput))
{
break;
}
yield return 0;
}
dialogueEnded = false;
letterIsMultiplied = false;
dialogueText.text = "";
}
}
public void DropDialogue()
{
dialogueGUI.SetActive(false);
dialogueBoxGUI.gameObject.SetActive(false);
}
public void OutOfRange()
{
outOfRange = true;
if (outOfRange == true)
{
letterIsMultiplied = false;
dialogueActive = false;
StopAllCoroutines();
dialogueGUI.SetActive(false);
dialogueBoxGUI.gameObject.SetActive(false);
}
}
}