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 /
This question was closed Oct 07, 2014 at 05:06 AM by fafase for the following reason:

Duplicate Question

avatar image
0
Question by devstudents · Oct 07, 2014 at 04:43 AM · inheritanceinstances

The best way to create instances of an enemy class

I have studied object oriented programming and unity in general for a while now. However I'm still not sure how to accomplish the following:

Say I make a base character class that contains all the attributes shared by both players and enemies (strength, dex, intelligence etc.) and I want to create unique instances of this class and attach those instances to game objects. These instances could in turn be used by other scripts that control combat etc. It's vital that the instances have differing values on the attributes contained in the base character class. What is the best way to create instances like that?

I couldn't work out how to do this so I just made two separate scripts that basically did exactly the same thing and attached those scripts to the player and enemy game objects. Very bad coding practice, I know, but I'm not sure how to use inheritance etc. as surely the script would have to inherit from monobehaviour if I am going to attach it to an object?

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

  • Sort: 
avatar image
0
Best Answer

Answer by Limbo · Oct 07, 2014 at 05:04 AM

I think you just want to be inheriting like so C#

 public class BaseClass : MonoBehaviour
 {
     public int health;
 }

so that's the class the both the player and enemy can/will share, let's say now you want to create the player script

 public class Player : BaseClass
 {
     void Start()
     {
          Debug.Log("Health: " + health);
     }
 }

now your player has health because BaseClass has it too

Comment
Add comment · Show 4 · 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 devstudents · Oct 07, 2014 at 05:09 AM 0
Share

so you attach the baseClass to the player and enemy game object? And then you create separate classes that inherit from baseClass for every instance of the class that you want? How do you attach those specific instances to game objects or do you need to?

avatar image fafase · Oct 07, 2014 at 05:16 AM 0
Share

Seems like the link you need is not the one @Landern provided but more this one http://msdn.microsoft.com/en-us/library/vstudio/ms173149(v=vs.100).aspx

avatar image devstudents · Oct 07, 2014 at 06:18 AM 0
Share

ah man! I finally understand. $$anonymous$$y problem was that I got it stuck in my head that in order for a class to be attached to a gameobject it needed to inherit directly from monobehaviour. But if it inherits from a class that inherits from monobehaviour, then it inherits from monobehaivour!!!! Ah dude. That took me a long time to work out. haha.

avatar image Limbo · Oct 07, 2014 at 01:48 PM 0
Share

Yes, you only attached Player.cs to you gameObject and your component will be something like this Player<-BaseClass<-$$anonymous$$onoBehaviour so you are really attaching a script component that in it's lowest level is a $$anonymous$$onoBehaviour... well $$anonymous$$onoBehaviour also Inherited from Object... but yeah you got it right.

avatar image
0

Answer by Landern · Oct 07, 2014 at 04:44 AM

@bunny83 addressed this a while ago, please read: http://answers.unity3d.com/questions/119516/inheriting-from-a-class-that-inherits-from-monobeh.html

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Polymorphism question for my custom class 1 Answer

Public variables from another class's nested class 1 Answer

How to import the object from server to unity 2 Answers

Find all instances of a script in the scene? 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