Error use of unassigned local variable

Hi,

i need to fill a array with a for loop for each item in an xml file.

Within in the forloop by the first line it’s says “use of unassigned local variable items”
I tried everything and cannot find a solution.

Anyways here the code:

public Item[] loadXmlFile(string filename,string tagname)
    {
        Item[] items;
        TextAsset file;
        file = Resources.Load(filename) as TextAsset;
        XmlDocument xmlFile = new XmlDocument();
        xmlFile.LoadXml(file.text);
        XmlNodeList listNodes = xmlFile.GetElementsByTagName(tagname);

        for (int i = 0; i <= listNodes.Count; i++)
        {
            items_.itemName = listNodes*.Attributes["itemname"].Value;*_

items_.prefab = (Transform)Resources.Load(listNodes*.Attributes[“prefab”].Value);
items.icon = (Texture2D)Resources.Load(listNodes.Attributes[“icon”].Value);
items.buildCost = listNodes.Attributes[“buildcost”].Value;
items.maxInhab = listNodes.Attributes[“maxInhab”].Value;
items.elecConsump = listNodes.Attributes[“elecconsump”].Value;
items.waterConsump = listNodes.Attributes[“waterconsump”].Value;
items.xpAdd = listNodes.Attributes[“xpadd”].Value;
items.itemText = listNodes.Attributes[“itemtext”].Value;
}
return items;
}*_

You should initialize the array with a specified number of elements.

Use:
Item items = new Item[sizeHere];

Then you can populate the array.

Maybe I’m being silly but just like the error says “items” is not initialized?

//this will be before the for loop
items = new item[listNodes.Count];

And by the way, since ‘i’ starts at 0 you dont go to “i<=listNodes.Count” but to “i< listNodes.Count” right?