- Home /
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
$$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.
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?
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.
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.
@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
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.