- Home /
Text to Variables
Hi, I want to make a script that reads a text file and puts each line into a variable, something like this:
Text file:
number1
number2
Script:
var number1 = textline 1
var number2 = textline 2
Something like that, but i dont know how, please help me.
Answer by Kleptomaniac · Mar 04, 2012 at 12:14 AM
I'm not sure if this is the best way to do it, but we shall see. You could use StreamReader, one of the System.IO classes. Eric5h5 does a demonstration on how to utilise lines from a .txt file in this post.
From his code, all you would need to do is add your variables var number1 : String; and var number2 : String; to the beginning of the script and then, at the end of the Start() function replace
for (line in lines) {
print (line);
with:
number1 = lines[1];
number2 = lines[2];
//any further variables you have
That's basically it. If you have a large amount of lines you want to pass through variables, you would probably need a for loop of some kind to make life simpler. However, I'm not so great with for loops to be honest (barely ever used them), so I probably wouldn't be able to help you on that one. ;)
Hope that helps, Klep
Your answer
Follow this Question
Related Questions
[Networking] Changed text does not appear changed for players who just connected 0 Answers
How to make a Score GUI. 0 Answers
How do I bring variables from two scripts to one then have them print on a canvas text? 0 Answers
How to display health on screen as text? 1 Answer
General question/advice for displaying multiple variables on a gameobject 0 Answers