Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 23, 2017 at 02:29 PM by jonander.
avatar image
0
Question by jonander · Mar 18, 2016 at 12:35 PM · scripting problemerrortextxmlxmlserializer

I dont understand why the script to read .xml doesnt works.

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--

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by BIKTOP · Oct 13, 2016 at 10:54 AM

@jonander Same problem. Do you found a solution ?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer

Need Help With XML Reading Class Initialization 1 Answer

Text component change through script not working 1 Answer

Need help with XML Parsing. (C sharp) 1 Answer

Error while deserializing an xml file 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges