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 Anatsol · Jan 09, 2013 at 12:21 PM · inheritanceclassesstructoop

I have objects that have the same set of variables, but different desired behaviors. How can I do this without repeating variable declarations across all behaviors?

I have a fairly sizable number of bullets in my game. Some will do things like seek the player out, some will do other things, but all of them will have a set of properties(speed, power, boolean(isPiercing), etc.)

My OOP-fu is a little weak, but I've wondered if inheritance might be the way to go. I'm not entirely sure how inheritance works with Unity, though.

I suppose I could make one script containing all of the variables, and assign them to each bullet prefab I have, and have all of their behavior scripts reference the other script attached to the object, but that seems like a really awkward and wasteful way of doing it. Is there a better way to do this using inheritance, or something else I might not be aware of?

Note: Currently using UnityScript as my language.

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

Answer by Piflik · Jan 09, 2013 at 12:29 PM

Inheritance is exactly what you need. Create a class, just like you normally would, put all your shared variables and methods in there. Then you create your actual bullet classes. Instead ov MonoBehaviour, you'd then extend your super-class.

UnityScript implicitely extends MonoBehaviour unless you do the class declaration yourself.

Not sure about the actual syntax, since I only ever did this in C#, but something along the lines of:

 public class Bullet01 extends BulletBase {
     //code here
 }

You then only assign the sub-class to the bullet. All inherited variables will show up in the inspector.

Comment
Add comment · Show 2 · 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 Anatsol · Jan 09, 2013 at 01:53 PM 0
Share

Alright, that's what I expected. But how does that work script wise?

For instance, should I have bulletBase.JS contain the public class BulletBase, then declare bullets that extend bullet base in each script?

$$anonymous$$g.

BulletBase.js:

 public class BulletBase extends $$anonymous$$onoBehavior{
 //Variables(e.g, Speed), object cleanup, other code.
 }

SeekingBullet

 public class SeekingBullet extends BulletBase{
 //code that follows player, at Speeds defined in through Inspector thanks to BulletBase?
 }


Then I should attach SeekingBullet.js to a prefab for my seeking bullets, and leave BulletBase.js unattached?

avatar image Piflik · Jan 10, 2013 at 05:32 PM 0
Share

Exactly. The sub-class will inherit all properties of the super-class and they will show up in the inspector. You SeekingBullets script will first show all variables of the BulletBase class and then all of its own variables. The super-class doesn't have to be assigned to an object.

You can of course use the BulletBase script for the most basic bullets, if you want to, and extend it with the sub-classes for the special bullets. All variables/methods of the super-class that you want to have differently in the sub-classes would then have to be overriden.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Update Function Work Around Question 2 Answers

FPS weapons class structure 1 Answer

Calling a method of a class that is a part of another class? 1 Answer

Does Unity support multiple Inheritance? 3 Answers


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