- Home /
Load txt file into a class
Hi. I have a .txt file that looks like this
Name A
1
2
3
Name B
3
2
1
Name C
11
22
33
The number of objects in this .txt file will change, and I want a way to load these into a struct.
var itemDataBase : TextAsset;
var itemList : String[]; // i don't wanna have to set a specific array size
class itemStruct{
var name;
var cost;
var health;
var damage;
}
public var item : itemStruct[]; // i don't wanna have to set a specific array size
function Start()
{
if(itemDataBase)
{
var itemCount = 0;
itemList = (itemDataBase.text.Split("\n"[0]));
for(line in ) // need this to loop once for every line in .txt file
{
item[itemCount].name = itemList[line++];
item[itemCount].cost = itemList[line++];
item[itemCount].health = itemList[line++];
item[itemCount++].damage = itemList[line];
}
}
}
Thanks.
Comment
I'm not sure how to. http://answers.unity3d.com/questions/155003/item-database.html I was looking at this post, I want to do something similar.
Possibly creating a temporary class item inside the loop, then send that item to a function that creates an item that can be reached from anywhere.