Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Olgo · Nov 04, 2013 at 03:58 PM · inheritancederived-classinherited-members

Why the null reference?

Hello, I get a null reference exception in the following code:

the variable "itemInstance" is of class Item, soil inherits from Item.
However, when the code goes to check soil.contents[0] (a member of the inherited class) I get the error.

             if(isInitialized == false){
                 soil = itemInstance as Soil;
                 isInitialized = true;
             }
 
             if( soil.contents[0] != null ){   //<<---- NULL REFERENCE EXCEPTION HERE
                 Seed seed = new Seed();
                 seed = (Seed)soil.contents[0];
                 soilState = SoilState.fertile;
             }

Thanks!

EDIT: I should add that itemInstance is declared as an Item but a Soil was put in to it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jdyeager · Nov 04, 2013 at 04:07 PM

There are a few possibilities that I see:

  1. itemInstance is null

  2. isInitialized is true already (so soil never gets set)

  3. Soil is not a sub-class of Item

  4. soil.contents is null instead of an array of Seeds

Also, just a quick tip, you don't need to do:

if (isInitialized == false) { ... }

Since isInitialized is a boolean already you can just do:

if (!isInitialized) { ... }

Comment
Add comment · Show 1 · 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 Olgo · Nov 04, 2013 at 04:23 PM 0
Share

I think issue here is described at this link.

http://stackoverflow.com/questions/729527/is-it-possible-to-assign-a-base-class-object-to-a-derived-class-reference-with-a

I'm starting to learn more what the problem is; as I'm learning the proper names for everything I can figure how to Google it better.

I checked,

  1. itemInstance isn't null, I can debug.Log its name.

  2. isInitialized starts as false, thats how I declared it

  3. Soil inherits from Container, Container inherits from Item

  4. Soil soil = itemInstance; itemInstance is set as this GameObject is created in the following scripts:

    void Drop$$anonymous$$ouseItem(){ if(playerInventory.mouseItem != null){ if(Input.Get$$anonymous$$ouseButtonUp(0) && !mouseBusy){ mouseBusy = true; Vector3 dropPos = this.transform.TransformPoint( Vector3.forward * dropDistance ); dropPos = new Vector3(dropPos.x, dropPos.y + 1, dropPos.z); spawn.Item(playerInventory.mouseItem, dropPos); playerInventory.mouseItem = null; } } }

    public GameObject Item(Item newItem, Vector3 position){ GameObject go = Instantiate(Resources.Load("Item/" + newItem.prefabName), position, Quaternion.identity) as GameObject; go.AddComponent(); BoxCollider bc = go.AddComponent(); ItemBehaviour ib = go.AddComponent(); ib.itemInstance = newItem; return go; }

avatar image
0

Answer by Cains · Nov 04, 2013 at 04:10 PM

I'm not sure when this portion of the code runs in your project, but if it checks soil.contents[0] before you set soil equal to something it'll throw that error.

If it's not that I think it may be something in itemInstance, perhaps your contents array isn't being declared/populated or it just doesn't exist.

Comment
Add comment · Show 1 · 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 Olgo · Nov 04, 2013 at 04:38 PM 0
Share

It should exist. I have the following line in the constructor.

 contents = new Item[1];

However, I'm concerned it gets snipped in the process of being saved as an Item for 1 step of the way, but the way my code works I'm not sure how to bypass that step. See the link I linked jdyeager. I'm trying to figure out if that is the problem.

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

17 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

Related Questions

Help with using JsonUtility.FromJsonOverwrite 1 Answer

Make a field in inspector hold inheriting classes 2 Answers

How to access derived class members from base class type 1 Answer

Using functions of Inherited classes 3 Answers

Is the class derived/inherited from generic list serializable? 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