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
1
Question by Anatsol · Jan 10, 2013 at 01:55 PM · javascriptgetcomponentinheritancepolymorphism

How can I access an inherited method from a separate (collided) object?

So, in order to make development a bit easier, I decided to create custom classes for projectiles and enemies. The enemy class contains health and health related functions, and the projectiles contain damage variables which were previously passed into it.

Before I created the classes(and only had one type of enemy and one type of projectile, I simply had my bullet get the(predictably named) script component and simply call the function(and passing the damage as a parameter)

Hackishly retyped code, since I'm at a different computer.

Bullet.JS:

 OnColissionEnter( c : collision){
 if(c.tag == "Enemy"){
     c.getComponent(EnemyHealth.JS);
     EnemyHealth.doDamage(bulletDamage);
 }
 }



And the doDamage logic handled whether or not the enemy was destroyed by the impact or not, etc.

But now since each enemy is often its own subclass, the script name is different each time, so I'm having a problem figuring out how to access my doDamage method since a number of my enemies have unique script names based on their subclass.

Is there any way to somehow grab the first script, all scripts, or a dynamic script using getcomponent, is there a different way of doing this, or would redesigning it to not be dependent on getcomponent be a better idea?

Note: I'm using UnityScript.

Edited for spelling.

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

Answer by PAEvenson · Jan 10, 2013 at 02:26 PM

 var EnemyHealth= c.GetComponent<[BaseClassGoesHere]>();

You just need to make sure you have the doDamage function in the base class and overrides int the subclasses.

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 Anatsol · Jan 10, 2013 at 02:31 PM 0
Share

Had no idea you could just grab the base class. Thanks!

avatar image PAEvenson · Jan 10, 2013 at 02:33 PM 2
Share

yay for polymorphism! :)

avatar image Bunny83 · Jan 10, 2013 at 02:49 PM 0
Share

That's the point of having a base class ;) When an object is derived from a base class it has two "types". It's own type and the type of the base class. You can access and object by a base class reference but you can only access things which are defined in the base class since from the compilers point of view you just have a base class.

Virtual functions allows a derived class to "reimplement" the function, so when using a base class reference and execute the function you will execute the new function in the derived class.

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

10 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

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

[JS] - GetComponents (No appropriate version) 1 Answer

changing js public variable from cs script 2 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