Hello, I have been trying to grasp how you would go about reading variables from a text file that is for example in the root folder of the Unity Project. I would like to use this to change certain aspects of the game without having to rebuild it, for example change the color of a cube after the standalone containing it is already built.
Currently I have found this script from the forums, and it works perfectly at what it was intended to do, display the text lines in print on the console.
import System.IO;
var textName : String;
function Start () {
try {
// Create an instance of StreamReader to read from a file.
sr = new StreamReader(textName);
// Read and display lines from the file until the end of the file is reached.
line = sr.ReadLine();
while (line != null) {
print(line);
line = sr.ReadLine();
}
sr.Close();
}
catch (e) {
// Let the user know what went wrong.
print("The file could not be read:");
print(e.Message);
}
}
I have a text file in the root of my project with lines that go like
"
cube1 isGreen
cube2 isRed
cube3 isYellow
"
I also have a managerial script in the game that interprets the strings and causes them to change the colors of the objects in question. However I cant figure out how to take these lines, which are currently only printed, and put the information into a list that I can use to give to the manager script.
Ideally I would like for the “cube#” part to be left out, and only read the line number and the variable ie the first line in the array would say “isGreen”. Either that or It could have “var cubeOne : String;” and place the “isGreen” there, if an array would be too difficult.
If anybody would be willing to explain this to me I would be extremely appreciative!