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 getyour411 · Feb 08, 2015 at 08:28 PM · c#nullreferenceexceptioninheritance

Inheritance and this NRE

I'm getting an NRException and need some advise.

GameObject A/GameManager-esque object in Awake() populates list allFactions (debug on that shows 4, correct count).

GameObject B/BaseActor object in Start() sets a [protected] variable/reference to GObj A.allFactions and then calls function also on GameObject B

GameObject B/pcActor (inherits from B/BaseActor) yields an NRE on variable.allFactions.Count within the called function; pcActor via inheritance should have access to variable reference so why do I get the NRE? Some code:

GOBJ B:

 void Start() {

     qmload = GameObject.FindGameObjectWithTag ("GameManager").GetComponent<QM_LoadResources> ();

     if (this.gameObject.tag == "Player") {

             curHealth = maxHealth;
             content = curHealth + "/" + maxHealth;
             energyRegenRate = .5f;

             QM_PlayerCharacter pcActor = (QM_PlayerCharacter) GetComponent<QM_PlayerCharacter>();
             pcActor.AssignFactions();


         }

GOBJ B/inherits from above:

 public void AssignFactions() {
 
         Debug.Log ("Game factions count: " + qmload.allFactions.Count);
 
         for (int cnt=0; cnt < qmload.allFactions.Count; cnt++) {
                         
             if (qmload.allFactions [cnt].fKnowsOf) {
                 pcFactions.Add (qmload.allFactions [cnt]);
                 pcFactions [cnt].fValue = 0;
             }
         }
 
         Debug.Log ("Known factions: "+pcFactions.Count);
 
         }


NRE comes on GOBJ B/pcActor any use of qmload.allFactions

Edit: Initially, I erroneously said GameObject B and C, but BaseActor and pcActor are both on GameObject B

Comment
Add comment · Show 9
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 mattyman174 · Feb 09, 2015 at 06:00 AM 0
Share

$$anonymous$$ight seem trivial but are you sure Q$$anonymous$$_LoadResources is attached to the GO or that the tag is correct?

Start with the small stuff and work through it from there. I find more often that its the small and easy to forget stuff that creates more problems.

avatar image fafase · Feb 09, 2015 at 06:22 AM 0
Share

Well I can't really explain the NRE but I could point out some weird design. Since BaseActor is about to call something on a sub class, I would make the AssignFactions a abstract or virtual method that is overriden below.

In my view (and it could be wrong), parent class should not be aware of sub class members so if a parent class a subclass member, this one is a candidate for polymorphism.

$$anonymous$$y tow cents on your problem, are B and C the same object?

avatar image mattyman174 · Feb 09, 2015 at 06:26 AM 0
Share

In my view (and it could be wrong), parent class should not be aware of sub class members so if a parent class a subclass member, this one is a candidate for polymorphism.

I was thinking of pointing this out as well but thought it was a bit out of scope for the question.

avatar image fafase · Feb 09, 2015 at 06:44 AM 0
Share

I consider that out of topic advice might fix issues. And if not they may guide wanderers towards the light of excellency.

In this case, I see GO A, GO B and GO C with the assumption that a component on B should provide info on C based on the fact that the component on B is a parent class of C (the type only is). So, it sounds like those are two distinct objects so whatever B knows, C is oblivious of it.

avatar image getyour411 · Feb 10, 2015 at 02:23 AM 0
Share

@fafase and @mattyman174 Yes the design is odd and part of that is me shuffling things around to try to resolve the NR$$anonymous$$ Originally I did not have parent make any calls to extension/sub but I was still getting NRE and I thought maybe it was because BaseActor had not fully initialized before pcActor tried to start using it; script ordering did no good either; so I went this route.

Also, your comments made me correct my problem desc, GameObject B contains both BaseActor and pcActor (sorry about that).

Game$$anonymous$$anager has tag and Q$$anonymous$$_LoadResources is attached; there are many methods in other classes that call and use Game$$anonymous$$anger/Q$$anonymous$$_LoadResources

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Jul 26, 2015 at 03:15 AM

I changed a few of my "GameManager"-esque classes to the Singleton pattern with single instance safe guards and this went away.

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

21 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Null Reference Exception: Object Reference, with base class? 1 Answer

How to change variables from another script? 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