Hey guys,
I’m working on my Bachelor project right now and encountered a weird bug with my Dialogue script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextWriting : MonoBehaviour
{
[SerializeField] string dialogue;
[SerializeField] Text textAusgabe;
[SerializeField] GameObject textFenster, textFieldPressF;
bool visited, textWritten, stopWriting;
[SerializeField] float waitForSec;
private void FixedUpdate()
{
//Wenn Text Aufbau vorzeitig abgebrochen, dann fülle die Textbox sofort komplett aus
if (Input.GetKeyDown(KeyCode.F) && !textWritten)
{
stopWriting = true;
textAusgabe.text = "";
textAusgabe.text = dialogue;
}
//Wenn Text fertig geschrieben wurde, schließe das Dialog Fenster
else if(Input.GetKeyDown(KeyCode.F) && textWritten)
{
textFieldPressF.SetActive(false);
textAusgabe.text = "";
textFenster.SetActive(false);
StopAllCoroutines();
gameObject.SetActive(false);
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
if (other.CompareTag("Player") && !visited)
{
textAusgabe.text = "";
visited = true;
textWritten = false;
stopWriting = false;
textFenster.transform.localScale = new Vector3(1, 0, 1);
Coroutine animBox = StartCoroutine(AnimateTextbox());
}
}
IEnumerator AnimateText(string strComplete)
{
int i = 0;
textAusgabe.text = "";
while (i < strComplete.Length)
{
if (stopWriting)
{
break;
}
else
{
textAusgabe.text += strComplete[i++];
yield return new WaitForSeconds(waitForSec);
}
}
textWritten = true;
}
IEnumerator AnimateTextbox()
{
textFenster.SetActive(true);
for (float i = 0; i < 1; i += 0.1f)
{
textFenster.transform.localScale = new Vector3(1, i, 1);
yield return new WaitForSeconds(0.001f);
}
textFieldPressF.SetActive(true);
dialogue = dialogue.Replace("\
", "
");
Coroutine animText = StartCoroutine(AnimateText(dialogue));
}
}
So in my game I have a few Trigger which will show a in the inspector given dialogue to the player. The first dialogue is this one:
In the first room you should test the environment to help you out.
You can run with [Shift] and jump with [Space].
And if the player is pressing the F key to skip the “animation” for the scrolling text, the text box shows a text from another trigger later in my game (there are 3 other ones in the hierarchy before this one):
Now you’re ready to test yourself if you can combine all the learned skills from before. Look out for your environment and try to find a way to the top of the room. Sometimes things aren’t what they seem to be.
I just don’t get it why it is happening, I even tested to get the name of the OnTriggerEnter to see if there is another thing entering a trigger but it’s only the player himself and only once.
I hope you guys can help me out with that.