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 /
  • Help Room /
avatar image
0
Question by Tespy · Dec 26, 2016 at 07:24 PM · unity5shooterpolymorphism

How to get proper version of script from another object?

So, I have been trying to make a minimalistic shooter to improve my skills with Unity (version 5.0.0f4). The problem I’m trying to solve is how the player bullets don’t do damage to the boss. Looking at the debug logs, I assume this has to do with how the Boss class inherits from the __Enemy class, which itself inherits from the MovingEntity class, which is what the bullet accesses right before doing damage.

So, how do I programmatically make the bullet access the proper script instance the target has, so it does damage to the actual enemy instance? As it is now, the debug logs say it did damage, but the editor says the boss is still at full HP. I could just use an if-else statement, but I’m sure there must be a better way.

Here is the code for the bullet’s collision:

 protected virtual void OnTriggerEnter2D(Collider2D collision)
     {
         Debug.Log(this.name + " is colliding with " + collision.name);
 
         //if this bullet hits something it is meant to hit, do damage to it
         if (collision.tag.Contains(TargetTag)) //
         {
             Debug.Log(this.name + " damaging a(n) " + TargetTag + "! Target: " + collision.name);
             MovingEntity en = collision.gameObject.GetComponent<MovingEntity>();
             en.TakeDamage(this.Damage);
             Destroy(this.gameObject);
         }
 
     }

Extra info, in case it helps:

I also have an Asteroid class, which inherits from __Enemy. The bullets damage the Asteroid objects just fine, despite the prefabs not specifically having any instances of MovingEntity. The TakeDamage function is also virtual, so I know that lacking virtualization isn't the problem.

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

Answer by TBruce · Dec 26, 2016 at 07:25 PM

Try this to see where your problem lies

 protected virtual void OnTriggerEnter2D(Collider2D collision)
 {
     Debug.Log(this.name + " is colliding with " + collision.name);
 
     //if this bullet hits something it is meant to hit, do damage to it
     if (collision.tag.Contains(TargetTag)) //
     {
         if (collision.gameObject.GetComponent<MovingEntity>() != null)
         {
             Debug.Log(this.name + " damaging a(n) " + TargetTag + "! Target: " + collision.name);
             MovingEntity en = collision.gameObject.GetComponent<MovingEntity>();
             en.TakeDamage(this.Damage);
         }
         else
         {
             Debug.Log("GameObject " + collision.gameObject.name + " with the tag " + collision.gameObject.tag + " does not have an attached GameObject of type MovingEntity. Please add one and try again");
         }
 
         Debug.Log("Destroying the current object - " + this.gameObject.name);
         Destroy(this.gameObject);
     }
 }
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 TBruce · Dec 27, 2016 at 07:35 PM 0
Share

Hi @Tespy, Did this help solve your problem?

avatar image Tespy TBruce · Feb 05, 2017 at 06:51 PM 0
Share

I'm really sorry for the late reply; I don't know how I missed your answer until now ._. Anyway, I did find my own solution. I just tested yours, though, and it works, too. So, thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Survival Shooter problems 0 Answers

UNITY shooting script does nothing 1 Answer

How do I make a script for camera recoil recovery? 0 Answers

Unity 5.1 networking 0 Answers

Tag doesn't work when gameObject is a child of the Main camera? 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