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
0
Question by Jogabba · Jul 04, 2016 at 09:48 PM · classparent-childclass instance

Noob question about class inheritance

I'm kinda new to scripting and I've got a basic question about class inheritance in Unity.

Let's say I want to make a base class named Enemy, which will contain the basic features shared by all enemies, and a derived class for every enemy type, for example, Orc and Goblin.

So I've got three different scripts: Enemy (the parent class), Orc and Goblin. And the question is: to which gameObjects should I attach them? I can only imagine the Orc and Goblin scripts must be attached to the Orc and Goblin prefabs, but where do I put the Enemy class?

Thanks in advance!

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
2
Best Answer

Answer by LMan · Jul 04, 2016 at 10:06 PM

Simply put, you don't put the Enemy class anywhere- it comes with the inheriting classes automatically. Because Orc and Goblin inherit from Enemy, they get access to all the member variables of Enemy in addition to their own specific variables.

For instance, suppose all your enemies have an integer variable health. You can put health into the Enemy base class, and every class that inherits Enemy will get that variable. On the other hand, if only your Goblins have a float variable magic, you would put magic into the Goblin class. Only the Goblins would have that variable.

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 Jogabba · Jul 04, 2016 at 11:01 PM 0
Share

Thank you for your really quick answer! For some reason I assumed had to be attached to a gameObject in order to do something, but you're right: if I make my Orc class (attached to the Orc prefab) extend from the Enemy class (which is not attached to anything) it works anyway. Thanks! :)

avatar image
1

Answer by Bunny83 · Jul 04, 2016 at 10:10 PM

The answer is: nowhere. The Enemy class is just the base class. Every derived class is also an Enemy. If the Enemy class itself should not be used on it's own you should make it an abstract class. That way it can't be attached to a GameObject.

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 Jogabba · Jul 04, 2016 at 11:10 PM 0
Share

Thank you for your answer! I'll make some research about abstract classes! Yet, I don't know if it's convenient that they can't be attached to GameObjects in my case:

I have a function that instantiates enemies, and gives them a certain HP value, which is a variable defined in the Enemy Class. I do this with this line of code:

(EnemyInstance.GetComponent("EnemyBehaviour") as EnemyBehaviour).HPValue= 100;

To access this HP value, I have to attach the Enemy Class to the enemies. This way, the Orc has both the Enemy and the Orc class scripts attached, and the Goblin has both the Enemy an the Goblin class scripts attached.

(By the way, all enemies have the same HP, doesn't matter if they're Orcs or Goblins.)

$$anonymous$$aybe there's a more proper way to do this?

avatar image Bunny83 Jogabba · Jul 05, 2016 at 01:32 AM 0
Share

No, you still haven't understood what inheritance mean. When you attach an "Orc" script to your object that Orc instance is also an Enemy instance.

So if the gameobject "obj" only has an Orc script attach you can still do:

 GameObject obj;
 
 obj.GetComponent<Enemy>().HPValue = 100;

Since the "Orc" script is derived from "Enemy" you can treat an Orc as Enemy. So the GetComponent call will return the Orc instance as Enemy.

Attaching an Orc script as well as an Enemy script to the same object doesn't make any sense since everything inside the Enemy class will be present twice. Also GetComponent<Enemy>() could return either of the two instances since both are Enemy instances.

avatar image Jogabba Bunny83 · Jul 05, 2016 at 06:10 PM 0
Share

You're right, it works! Thank you for your answer, sir!

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

45 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

Related Questions

Will calling and instance of classA, from classA, repeatedly, cause a stack overflow? 1 Answer

Does attaching a derived class script to a gameobject create a new instance of its parent class specific to that gameobject? 0 Answers

Putting a Child Class into a List of Parent Type and then casting it back to a Child 0 Answers

Array of Objects of a Class 1 Answer

How to pass classes of a class to a function's parameter? 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