Hi, I’m having another problem , it’s not an error, but I wan’t text to display on a text box like a type writer, and the result I get is it immediately shows all text at once and has random letters appear at the end. It looks like this:
and the last letter keeps changing multiple times each for every sentence that is said.
Here are my three scripts that tie into the text box:
TextImporter
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextImporter : MonoBehaviour {
public TextAsset textFile;
public string[] textLines;
// Use this for initialization
void Start () {
if (textFile != null) {
textLines = (textFile.text.Split('
'));
}
}
}
ActivateTextAtLine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActivateTextAtLine : MonoBehaviour {
public TextAsset theText;
public int startLine;
public int endLine;
public TextBoxManager theTextBox;
public bool requireButtonPress;
private bool waitForPress;
public bool destroyWhenActivated;
// Use this for initialization
void Start () {
theTextBox = FindObjectOfType<TextBoxManager> ();
}
// Update is called once per frame
void Update () {
if (waitForPress && Input.GetKeyDown (KeyCode.J)) {
theTextBox.ReloadScript (theText);
theTextBox.currentLine = startLine;
theTextBox.endAtLine = endLine;
theTextBox.EnableTextBox ();
if (destroyWhenActivated) {
Destroy(gameObject);
}
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.name == "Player") {
if (requireButtonPress) {
waitForPress = true;
return;
}
theTextBox.ReloadScript (theText);
theTextBox.currentLine = startLine;
theTextBox.endAtLine = endLine;
theTextBox.EnableTextBox ();
theTextBox.isActive = true;
if (destroyWhenActivated) {
Destroy(gameObject);
}
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.name == "Player") {
waitForPress = false;
}
}
}
TextBoxManager
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class TextBoxManager : MonoBehaviour
{
public GameObject textBox;
public Text theText;
public TextAsset textFile;
public string[] textLines;
public int currentLine;
public int endAtLine;
public playerController player;
public bool isActive;
public bool stopPlayerMovement;
private bool isTyping = false;
private bool cancelTyping = false;
public float typeSpeed;
// Use this for initialization
void Start()
{
player = FindObjectOfType<playerController>();
if (textFile != null)
{
textLines = (textFile.text.Split('
'));
}
if (endAtLine == 0)
{
endAtLine = textLines.Length - 1;
}
if (isActive)
{
EnableTextBox();
}
else
{
DisableTextBox();
}
}
void Update()
{
if (!isActive)
{
return;
}
theText.text = textLines [currentLine];
if (Input.GetKeyDown(KeyCode.Return))
{
if (!isTyping)
{
currentLine += 1;
if (currentLine > endAtLine)
{
DisableTextBox();
}
else
{
StartCoroutine(TextScroll(textLines[currentLine]));
}
}
else if (isTyping && !cancelTyping)
{
cancelTyping = true;
}
}
}
private IEnumerator TextScroll(string lineOfText)
{
int letter = 0;
theText.text = "";
isTyping = true;
cancelTyping = false;
while (isTyping && !cancelTyping && (letter < lineOfText.Length - 1))
{
theText.text += lineOfText[letter];
letter += 1;
yield return new WaitForSeconds(typeSpeed);
}
theText.text = lineOfText;
isTyping = false;
cancelTyping = false;
}
public void EnableTextBox()
{
textBox.SetActive(true);
isActive = true;
if (stopPlayerMovement)
{
player.canMove = false;
player.anim.speed = 0;
}
StartCoroutine(TextScroll(textLines[currentLine]));
}
public void DisableTextBox()
{
textBox.SetActive(false);
isActive = false;
player.canMove = true;
}
public void ReloadScript(TextAsset theText)
{
if (theText != null)
{
textLines = new string[1];
textLines = (theText.text.Split('
'));
}
}
}
Here are the parameters: