Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by $$anonymous$$ · Jul 14, 2018 at 04:22 AM · variableinheritanceclasses

Getting the same variable from two different classes?

So let's say I have two classes, Pig and Wall. Pig inherits from Mob, which inherits from PhysicalObject, while Wall directly inherits from PhysicalObject. Now let's say I need to access a common variable from anything that inherits from PhysicalObject, which could be Mass. But I can't just SomeObject.GetComponent().Mass, as that would only work for the Pig class, and I can't say SomeObject.GetComponent().Mass, as I don't think I'll ever even attach that class to a gameObject because it is purely for inheritance purposes. Could I somehow do a check to see if whatever class I'm dealing with inherits from PhysicalObject somewhere down the line?

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

2 Replies

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

Answer by Bunny83 · Jul 15, 2018 at 02:56 AM

I think you don't really understand what inheritance means. If Pig and Wall are derived from PhysicalObject they are PhysicalObjects, just more concrete types. You can simply do

 float mass = SomeObject.GetComponent<PhysicalObject>().Mass;

This will work even when the actual class is a Pig or Wall instance or anything else derived from PhysicalObject. That's the main purpose of OOP to treat a concrete class in a more general / generic manner.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Jul 15, 2018 at 04:27 AM 0
Share

All I was thinking about was the fact that PhysicalObject wasn't going to be attached to the Pig gameobject, but I didn't realize that Pig class actually IS the PhysicalObject class.

avatar image
1

Answer by JVene · Jul 14, 2018 at 02:16 PM

If PhysicalObject owns Mass, you certainly can:

 a = SomeObject.Mass;

On Pig and Wall. Since you said that a common variable from PhysicalObject could be Mass, and through Mob Pig gets it just as Wall does.

Since Mass is a member of PhysicalObject, I see no reason for GetComponent, as it's a member of PhysicalObject, not a component on a gameObject, if I follow your description as stated.

Comment
Add comment · Show 5 · 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 ShadyProductions · Jul 14, 2018 at 03:58 PM 0
Share

Aslong as the $$anonymous$$ass property/field is public then you can access it from any derived class.

avatar image $$anonymous$$ · Jul 14, 2018 at 04:32 PM 0
Share

I'm sorry for the confusion, Pig and Wall will indeed be attached to gameObjects of the same name. So I'd be trying to get the $$anonymous$$ass variable from an instantiated class such as Pig or Wall.

avatar image JVene $$anonymous$$ · Jul 14, 2018 at 04:59 PM 0
Share

If $$anonymous$$ass is a member of PhysicalObject, then access is available to Pig and Wall as given.

If you're getting mass (lower case m) from the Rigidbody component of the gameObject that owns the script, then it is still available to both Pig and Wall through gameObject.GetComponent ().mass, since PhysicalObject would have to be derived from $$anonymous$$onoBehaviour, which provides the gameObject member from which to get the component.

avatar image $$anonymous$$ JVene · Jul 14, 2018 at 11:34 PM 1
Share

But assu$$anonymous$$g I don't know what the class is, just that it inherits from PhysicalObject, how would I access the $$anonymous$$ass variable?

 Object$$anonymous$$ass = Object.GetComponent<SomeUnkownClassThatInheritsFromPhysicalObject>().$$anonymous$$ass;
Show more comments

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

96 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 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 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 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 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

HideInInspector with inherited variables 1 Answer

Access to a variable inside a C# class 2 Answers

How to build up the class structure for an object 1 Answer

Private variable is not saved 3 Answers

Inheriting from classes from other files (Javascript) 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