- Home /
Inheritance in C# and Unity3d
So in my base class, I have a variable that is :
public GameSystem GS; // reference to a GameSystem
// Use this for initialization
void Start () {
GS = (GameSystem)GameObject.Find("Manager").GetComponent("GameSystem");
}
Why is GS not available to be used in the sub classes? I get an error of :
NullReferenceException: Object reference not set to an instance of an object TargetControl.OnMouseDown () (at Assets/Scripts/TargetControl.cs:37) UnityEngine.SendMouseEvents:DoSendMouseEvents()ne.SendMouseEvents:DoSendMouseEvents()
But if I made functions inside of my base class, they are available to my sub-classes. Why is GS not?
Can I interest you in using the generic version of GetComponent? GS = (GameSystem)GameObject.Find("$$anonymous$$anager").GetComponent (); It's significantly faster and less likely to promote runtime errors.
Answer by burnumd · Feb 17, 2011 at 05:31 PM
The error you're getting seems more like GS is not getting set. Are you overriding Start in your subclass and not calling base.Start ()?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Question about Inheritance in Unity 1 Answer
Pokemon declaring types 1 Answer
Base class variables not initialized for sub class? 1 Answer