x


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?

more ▼

asked Feb 17 '11 at 05:23 PM

RoR gravatar image

RoR
346 73 86 97

Can I interest you in using the generic version of GetComponent? GS = (GameSystem)GameObject.Find("Manager").GetComponent (); It's significantly faster and less likely to promote runtime errors.

Feb 17 '11 at 05:37 PM burnumd
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 ()?

more ▼

answered Feb 17 '11 at 05:31 PM

burnumd gravatar image

burnumd
3.4k 22 35 71

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x414
x162
x143

asked: Feb 17 '11 at 05:23 PM

Seen: 1707 times

Last Updated: Feb 17 '11 at 05:23 PM