Getting A Variable From a Outside Text File

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!

My first thought would be to switch your text file into a more ordered system, like a CSV spreadsheet. Perk to this is that it’ll be easier to modify using excel, instead of dealing with a text file. Here’s a script I’m using to do just that, creating a dictionary with the first CSV column as the key, and everything else stacked behind it. Right now I’m using this to attach about 20 attributes to gameobjects, so it works well :slight_smile: It skips duplicate keys and lines with an empty first column.

//Load a dictionary with spreadsheet data, taking a text file and a dictionary as arguments.
function LoadCSVfromTXT(csvAsTxt : TextAsset, dictionary : Dictionary.<String, List.<String> > ){

	var tempArray : String[];
	var tempArray2 : String[];
	
	//Split text file by lines
	tempArray = csvAsTxt.text.Split("

"[0]);

	//For as many lines as exist, split by commas. If the first value isn't already in the dictionary,
	//and isn't n/a, add it to the array.
	for(var x=0; x<tempArray.Length; x++){
	
		tempArray2 = tempArray[x].Split(","[0]);
		
		if(tempArray2[0] in dictionary || tempArray2[0]=="") continue;
		else{
			dictionary[tempArray2[0]] = new List.<String>();
			for(var y=1; y<tempArray2.Length; y++){
				dictionary[tempArray2[0]].Add(tempArray2[y]);
			}
		}
	}
}