- Home /
Question by
Ejpj123 · Mar 18, 2018 at 01:13 AM ·
2dcharactererror-messagetext box
Trying to make a textbox typewriter, I am having multiple errors C#
Hi, I'm trying to make a text box, and for some reason I am having tons of errors that have to do with displaying character by character on the box:
using System.Collections;
using System.Collections.Generic;
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('\n'));
}
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('\n'));
}
}
}
Will someone please help me? Here are the errors:
capture.png
(41.6 kB)
Comment
Best Answer
Answer by KittenSnipes · Mar 18, 2018 at 02:27 AM
This is the edited code. The main problem was with your curly brackets. If they are not placed correctly it can cause chaos throughout the code. The other problem is because of a different script (OnTriggerExit2D).
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('\n'));
}
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('\n'));
}
}
}
Your answer
Follow this Question
Related Questions
Flip 2D Character with Keys without moving 2 Answers
How can I move the character like 2D games? 1 Answer
Flat 2d image on 3d character. 0 Answers