Hi there. I wrote the code which should be triggered by trigger but it works before I trigger the trigger.
It’s just that I think I wrote everything by logic, but apparently, I did not. Thanks in advance if you help me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
/// <summary>
/// Simple dialogue system. No choices.
/// </summary>
public class Dialogue : MonoBehaviour
{
[SerializeField] TextMeshProUGUI theText;
[SerializeField] string[] sentences;
[SerializeField] float typeSpeed = 0.02f;
[SerializeField] GameObject continueButton;
int index;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player" && Input.GetKeyDown(KeyCode.E))
{
StartCoroutine(StartDialogue());
}
}
private void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
continueButton.SetActive(false);
}
}
public void NextSentence()//button func
{
continueButton.SetActive(false);
if (index < sentences.Length - 1)
{
index++;
theText.text = "";
StartCoroutine(StartDialogue());
}
else
{
theText.text = "";
continueButton.SetActive(false);
}
}
IEnumerator StartDialogue()
{
foreach (char letter in sentences[index].ToCharArray())
{
theText.text += letter;
if (theText.text == sentences[index])
{
continueButton.SetActive(true);
}
yield return new WaitForSeconds(typeSpeed);
}
}
}
Alright, will try it out . But the thing is, coroutine is started BEFORE I even enter a trigger.
– conguerrorIt actually helped. Thanks a lot.
– conguerror