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 garmekain · Mar 18, 2016 at 12:38 PM · instanceclasses

Cannot get variable from another class. Error: NullReferenceException

I want to have a variable containing all game items inside the Item class, but when I try to get the value of GameItemContainer.Container.items it gives me an error.

Item.cs:

 public string Name;
 public float Price;

 // Contructors
 public Item() {}
 public Item(string itemName)
 {
     List<Item> allItems = GameItemContainer.Container.items;
     for (int i = 0; i < allItems.Count; i++) {
         if (allItems[i].Name.ToLower() == itemName.ToLower()) {
             this.Name = allItems[i].Name;
             this.Price = allItems[i].Price;
             break;
         }
     }
 }


GameItemContainer.cs:

 [XmlArray("Items"),XmlArrayItem("Item")]
 public List<Item> items = new List<Item> ();


 public static GameItemContainer Container = LoadXML("gameItems");

 private static GameItemContainer LoadXML(string xmlname) {
     return GameItemContainer.Load (Path.Combine (Application.dataPath, "Scripts/XML/" + xmlname + ".xml")) as GameItemContainer;
 }

 public static GameItemContainer Load(string path)
 {
     XmlSerializer serializer = new XmlSerializer(typeof(GameItemContainer));
     using(var stream = new FileStream(path, FileMode.Open))
     {
         return serializer.Deserialize(stream) as GameItemContainer;
     }
 }


Error appears in line

 List<Item> allItems = GameItemContainer.Container.items;

And it throws the error

 NullReferenceException: Object reference not set to an instance of an object


Any help would be greatly appreciated.

Comment
Add comment · Show 2
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
avatar image Verzoto · Mar 18, 2016 at 04:03 PM 0
Share

Do you instantiate the GameItemContainer class? You need to instantiate to have access to class atributes.

avatar image garmekain Verzoto · Mar 18, 2016 at 04:26 PM 0
Share

How can I do that?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Magius96 · Mar 18, 2016 at 04:26 PM

Perhaps you should make your entire GameItemContainer class static as well as all members inside it.

Comment
Add comment · Show 2 · 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
avatar image garmekain · Mar 18, 2016 at 04:32 PM 0
Share

I cant because that way I cant read the X$$anonymous$$L file. Xml data is not getting stored in the variables.

avatar image garmekain garmekain · Mar 18, 2016 at 04:34 PM 0
Share

I need to have a container and the items inside that Container because it is the Container who stored the X$$anonymous$$L data

avatar image
0

Answer by Soraphis · Mar 18, 2016 at 05:20 PM

Im not sure if this will solve your problem but you should consider a singleton approach, something like this:

 public class GameContainer{
 
     private static GameContainer instance;
     public static GameContainer Instance{
         get{ 
             if(instance == null) instance = new GameContainer();
             return instance;
         }
     }
     
     // constructor should be private
     private GameContainer(){
           // load from XML
     }
 
      public List<Item> Items = new List<Item> ();
 
 }

you would call it like: GameContainer.Instance.Items, and Instance should never be null, because in this case a new instance is created.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instances of Classes 1 Answer

How to store data in script and attach it later? 0 Answers

Object Instance Error Message Confusion 1 Answer

Creating new instances of classes 2 Answers

Add an instance of a script as a component? 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