- Home /
Unity 2D - Text UI and Textbox scripting issue - HELP ASAP!
Hi, I'm new to Unity, coding and game development. I was trying to make a textbox system, and something isn't working, and it's not allowing my .txt file lines to be written in the Text UI object. It's just displaying default text, and not displaying what I wanted... Please help? :)
I cannot figure out what is wrong... :(
using UnityEngine;
using System.Collections;
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;
// Use this for initialization
void Start ()
{
player = FindObjectOfType<PlayerController>();
if (textFile != null)
{
textLines = (textFile.text.Split('\n'));
}
if(endAtLine == 0)
{
endAtLine = textLines.Length - 1;
}
}
void update()
{
theText.text = textLines[currentLine];
if (Input.GetKeyDown (KeyCode.Return))
{
currentLine += 1;
}
}
}
Answer by $$anonymous$$ · Apr 15, 2016 at 06:55 AM
I would suggest some improvements of your code:
assign a start value to currentLine before you use it the first time
Add theText.text = textLines[currentLine] into the if statement. This avoids adding the text each frame
Use Input.GetKeyUp to avoid going through all the lines real quick when you are holding the return key to long down. I guess you want to move 1 further only ones per key push
add code to limit currentLine to the maximum number of lines available.
Use Update instead of update as it must be used with a capital U.
Answer by chetanmac · Apr 15, 2016 at 06:56 AM
Please refere this link: http://answers.unity3d.com/questions/279750/loading-data-from-a-txt-file-c.html I think it will solve your problem!