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 RoR · Feb 17, 2011 at 11:39 AM · programmingnullreferenceexceptioninheritance

Question about Inheritance in Unity

I am using inheritance (C#) with a base class that has

public GameSystem temp = (GameSystem)GameObject.Find("Manager").GetComponent("GameSystem");

and I want my subclasses to be able to use temp, but whenever i try to use temp i get this error

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 this works. But if I replace Debug.log with the base class object it errors.

    GameSystem temp = (GameSystem)GameObject.Find("Manager").GetComponent("GameSystem");
    Debug.Log(temp.gameTimer);
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

1 Reply

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

Answer by Jesse Anders · Feb 17, 2011 at 01:02 PM

You can use the generic version of GetComponent() instead, e.g.:

GameSystem temp = GameObject.Find("Manager").GetComponent<GameSystem>();

However, I don't think you can use Unity API functions such as Find() in field initializers or constructors (or at least you're not supposed to). Instead, you should perform initializations of that sort in Awake() or Start().

Not sure what you mean about the Debug.Log() part.

Comment
Add comment · Show 3 · 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 RoR · Feb 17, 2011 at 05:19 PM 0
Share

Intersting. It worked, thank you. So inheritance in Unity is not like normal inheritance in C#? Because the base class var is not quite available to base classes, but can be accessed if I go through Get Component. I figure since if the var is in the base class, I should be able to access it in the subclasses. But, I must use GetComponent as this is how Unity handles inheritance?

avatar image RoR · Feb 17, 2011 at 05:19 PM 0
Share

Why are functions in the base class available to the sub-classes, but not variables?

avatar image Jesse Anders · Feb 17, 2011 at 08:31 PM 0
Share

I'm not completely clear on your question, but there shouldn't be anything particularly unusual about how inheritance in C# works with respect to Unity. In other words, the behaviour should be the same as in any other C# application (AFAI$$anonymous$$, at least).

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

No one has followed this question yet.

Related Questions

Multiple Cars not working 1 Answer

Inheritance in C# and Unity3d 1 Answer

Grabbing an Inherited Variable 0 Answers

Destroying Elements From An Array 1 Answer

An OS design issue: File types associated with their appropriate programs 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