The .xml text: (name) items.xml
<?xml version="1.0" encoding="UTF-8"?>
<ItemCollection>
<Items>
<Item texto11="INTRODUCE AQUI EL TEXTO 1 (PANEL 1)"></Item>
<Item texto12="INTRODUCE AQUI EL TEXTO 2 (PANEL 1)"></Item>
<Item texto21="INTRODUCE AQUI EL TEXTO 1 (PANEL 2)"></Item>
<Item texto22="INTRODUCE AQUI EL TEXTO 2 (PANEL 2)"></Item>
<Item texto31="INTRODUCE AQUI EL TEXTO 1 (PANEL 3)"></Item>
<Item texto32="INTRODUCE AQUI EL TEXTO 2 (PANEL 3)"></Item>
<Item texto41="INTRODUCE AQUI EL TEXTO 1 (PANEL 4)"></Item>
<Item texto42="INTRODUCE AQUI EL TEXTO 2 (PANEL 4)"></Item>
</Items>
</ItemCollection>
C# Code1: (name) item.cs
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
public class Item {
[XmlAttribute("texto11")]
public string texto11;
[XmlAttribute("texto12")]
public string texto12;
[XmlAttribute("texto21")]
public string texto21;
[XmlAttribute("texto22")]
public string texto22;
[XmlAttribute("texto31")]
public string texto31;
[XmlAttribute("texto32")]
public string texto32;
[XmlAttribute("texto41")]
public string texto41;
[XmlAttribute("texto42")]
public string texto42;
}
This one is loaded with a empty GameObject: (name) ItemContainer.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
[XmlRoot("ItemCollection")]
public class ItemContainer {
[XmlArray("Items")]
[XmlArrayItem("Item")]
public List<Item> items = new List<Item>();
public static ItemContainer Load(string path)
{
TextAsset _xml = Resources.Load<TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(ItemContainer));
StringReader reader = new StringReader(_xml.text);
ItemContainer items = serializer.Deserialize(reader) as ItemContainer;
reader.Close();
return items;
}
}
The last Script. This one is loaded with a empty GameObject:
(name) ItemLoader.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ItemLoader : MonoBehaviour
{
GameObject Textop11;
Text textito11;
GameObject Textop12;
Text textito12;
GameObject Textop21;
Text textito21;
GameObject Textop22;
Text textito22;
GameObject Textop31;
Text textito31;
GameObject Textop32;
Text textito32;
GameObject Textop41;
Text textito41;
GameObject Textop42;
Text textito42;
public const string path = "items";
// Use this for initialization
void Start ()
{
Textop11 = GameObject.Find ("TextoP11");
textito11 = Textop11.GetComponent<Text>();
Textop12 = GameObject.Find ("TextoP12");
textito12 = Textop12.GetComponent<Text>();
Textop21 = GameObject.Find ("TextoP21");
textito21 = Textop21.GetComponent<Text>();
Textop22 = GameObject.Find ("TextoP22");
textito22 = Textop22.GetComponent<Text>();
Textop31 = GameObject.Find ("TextoP31");
textito31 = Textop31.GetComponent<Text>();
Textop32 = GameObject.Find ("TextoP32");
textito32 = Textop32.GetComponent<Text>();
Textop41 = GameObject.Find ("TextoP41");
textito41 = Textop41.GetComponent<Text>();
Textop42 = GameObject.Find ("TextoP42");
textito42 = Textop42.GetComponent<Text>();
ItemContainer ic = ItemContainer.Load(path);
foreach (Item item in ic.items)
{
textito11.text = item.texto11.ToString(); // ONLY THIS 1 LOAD
textito12.text = item.texto12.ToString(); //ERROR
textito21.text = item.texto21.ToString(); //ERROR
textito22.text = item.texto22.ToString(); //ERROR
textito31.text = item.texto31.ToString(); //ERROR
textito32.text = item.texto32.ToString(); //ERROR
textito41.text = item.texto41.ToString(); //ERROR
textito42.text = item.texto42.ToString(); //ERROR
}
}
}
…
ERROR:
NullReferenceException: Object reference not set to an instance of an object
ItemLoader.Start () (at Assets/Resources/ItemLoader.cs:70)
But only the First 1 Loads…
I can’t see the error.
–Jon Ander–