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 /
avatar image
0
Question by samdobon00 · May 07, 2020 at 06:22 AM · xmlnull reference exception

NullReferenceException: Object reference not set to an instance of an object (XmlDocument.LoadXml())

Hello! I have a problem with XML file when I press Start button on Unity:


NullReferenceException: Object reference not set to an instance of an object AbilitiesExtractor.extraerAbs () (at Assets/Scripts/Abilities/AbilitiesExtractor.cs:22) BattleGUI.Start () (at Assets/Scripts/TurnBasedCombat/BattleGUI.cs:19)


NullReferenceException: Object reference not set to an instance of an object BattleGUI.OnGUI () (at Assets/Scripts/TurnBasedCombat/BattleGUI.cs:30) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)


Here are the classes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Xml;
 using System.IO;
 
 public class AbilitiesExtractor : MonoBehaviour
 {
     public TextAsset abilityList;
     public static List<Ability> abilities = new List<Ability>();
     public List<Ability> extraerAbs()
     {
         XmlDocument xmlDocument = new XmlDocument();
         xmlDocument.LoadXml(abilityList.text);
         XmlNodeList itemList = xmlDocument.GetElementsByTagName("Ability");
         Debug.Log("Lista de habilidades: "+itemList.Count);
         for (int i = 0; i < itemList.Count; i++)
         {
             Ability temp = new Ability();
             temp.RequiredWeapon = itemList[i].SelectSingleNode("RequiredWeapon").InnerText;
             temp.RequiredSkill = itemList[i].SelectSingleNode("RequiredSkill").InnerText;
             temp.Range = int.Parse(itemList[i].SelectSingleNode("Range").InnerText);
             temp.Name = itemList[i].SelectSingleNode("Name").InnerText;
             temp.Description = itemList[i].SelectSingleNode("Description").InnerText;
             temp.Target = int.Parse(itemList[i].SelectSingleNode("Target").InnerText);
             temp.Power = int.Parse(itemList[i].SelectSingleNode("Power").InnerText);
             abilities.Add(temp);
         }
         return abilities;
     }
 }

And the second one:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BattleGUI : MonoBehaviour
 {
     private string playerName;
     private int playerLevel;
     private int playerTenacity;
     private List<Ability> abilities;
     AbilitiesExtractor absExtractor = new AbilitiesExtractor();
 
 
     void Start()
     {
         playerName = GameInformation.PlayerName;
         playerLevel = GameInformation.PlayerLevel;
         playerTenacity = GameInformation.Tenacity;
         abilities = absExtractor.extraerAbs();
     }
 
 
     void Update()
     {
         
     }
 
     void OnGUI()
     {
         GUI.Button(new Rect(0, 0, 0, 0), abilities[1].Name);
     }
 }

I have both of them attached to MainCamera, and the XML attached to AbilitiesExtractor. Here is the XML:

 <AbilityList>
     <Ability>
         <RequiredWeapon>Unarmed</RequiredWeapon>
         <RequiredSkill level="0">MartialArts</RequiredSkill>
         <Range>1</Range>
         <Name>Kick</Name>
         <Description>Kick that trash!</Description>
         <Target>1</Target>
         <Power>12</Power>
     </Ability>
     <Ability>
         <RequiredWeapon>Handgun</RequiredWeapon>
         <RequiredSkill level="0">Firearms</RequiredSkill>
         <Range>1</Range>
         <Name>Shot</Name>
         <Description>Shot that trash!</Description>
         <Target>1</Target>
         <Power>10</Power>
     </Ability>
 </AbilityList>


alt text

It is my first time working with XML on unity, but I have other clases working fine with other XML (objects), and I can't see the failure here. I will be very gratefull for any help you can give to me.

EDIT: If I test it on Awake method on the AbilitiesExtractor class (BattleGui removed), it run correctly. So the problem is calling the class, because class1(caller) can't reach xml attached to class2(called)? I don't know how it works. Here is the Awake method:

     void Awake()
     {
         extraerAbs();
         foreach(Ability ab in abilities)
         {
             Debug.Log(ab.Name + "\n" + ab.Description);
         }
     }

captura.png (63.9 kB)
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

127 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How do i load Xml and assign variables to player in C#? 1 Answer

How to type Color32 format into XML? 1 Answer

Displaying xml content with rich text 1 Answer

xml loading in unity andriod 1 Answer


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