Weird text field bug?
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("\\n", "\n");
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.\n\nYou 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.
Answer by GottesSchaf · Jan 12, 2020 at 04:55 PM
Alright, I found the answer myself. I was digging furthermore threw google and came up with the solution to use two int variables. One is getting inside the Start() the "Index Number" via thisObjInt = int.Parse(gameObject.name.Substring(0, 1);
and the other int variable is set when the player is walking into the Trigger with the same Method: checkInt = int.Parse(gameObject.name.Substring(0, 1));
(the name of the trigger object where the script sits has to have a integer number on the first place) Inside my Update the if statement is now this: if (Input.GetKeyDown(KeyCode.F) && !textWritten && thisObjInt == checkInt)
same for the else if. One last thing to mention is that when I pressed the F key the text was skipped immediately and the Textbox was closed again. To fix that I just set the "textWritten" bool after pressing F to skip and after the while loop inside AnimateText I ask if "i" is the same value as "strComplete.Length". If so textWritten is true.
Here is the whole script now: 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;
int thisObjInt, checkInt;
private void Start()
{
thisObjInt = int.Parse(gameObject.name.Substring(0, 1));
}
private void Update()
{
//Wenn Text Aufbau vorzeitig abgebrochen, dann fülle die Textbox sofort komplett aus
if (Input.GetKeyDown(KeyCode.F) && !textWritten && thisObjInt == checkInt)
{
Debug.Log("Dialog:"+dialogue);
stopWriting = true;
textAusgabe.text = "";
textAusgabe.text = dialogue;
textWritten = true;
}
//Wenn Text fertig geschrieben wurde, schließe das Dialog Fenster
else if(Input.GetKeyDown(KeyCode.F) && textWritten && thisObjInt == checkInt)
{
textFieldPressF.SetActive(false);
textAusgabe.text = "";
textFenster.SetActive(false);
StopAllCoroutines();
gameObject.SetActive(false);
}
}
private void OnTriggerEnter(Collider other)
{
Debug.Log(other.gameObject.name);
checkInt = int.Parse(gameObject.name.Substring(0, 1));
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)
{
Debug.Log("strComplete: "+strComplete);
int i = 0;
textAusgabe.text = "";
while (i < strComplete.Length)
{
if (stopWriting)
{
break;
}
else
{
textAusgabe.text += strComplete[i++];
yield return new WaitForSeconds(waitForSec);
}
}
if(i == strComplete.Length)
{
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("\\n", "\n");
Coroutine animText = StartCoroutine(AnimateText(dialogue));
}
}