Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How to prefill an input field in Unity C#?
Basically, I want the script to read a text file and put the text into a variable. Then, access the text element of a UI input field and put said text in there.
The referenced text file has one word per line, like this:
lineA
lineB
lineC
etc.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;
public class EditorController : MonoBehaviour
{
string oldText;
string[] newText;
public Text inputFieldText;
private void Start()
{
oldText = File.ReadAllText(@"C:\Users\Public\Documents/topicgeneratorelement1.txt");
inputFieldText.text = oldText;
}
}
Don't worry about newText - that's for later, when the code is more developed. Right now, I just want the input field to be filled.
I've also tried accessing the "text" component of the input field itself (e.g. "public InputField input;...input.text = oldText;), but that didn't work either.
2 Answers
- Anonymous4 years ago
That should work if you are actually getting a reference to the InputField using GetComponent<>();
Maybe you could use scriptable objects instead of approaching it this way, unless you need it to be a text file for some reason.