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 Tpaley · Dec 17, 2013 at 03:58 AM · collisiondestroygetcomponentdebugdamage

GetComponent on Collider not working

I'm working on a way to allow the player to kill monsters/destroy barricades. I have it mostly complete but for some reason, this one script isn't working how it should.

 void OnCollisionEnter (  Collision c   ){
     if (weaponType == WeaponType.Missile) {
         if (c.gameObject.GetComponent<Damageable>() != null) {
             Debug.Log("yes");
             Damageable damageable = c.gameObject.GetComponent<Damageable>();
             damageable.ApplyDamage( damage );
         }
         Destroy(this.gameObject);
     }
 }

I have a gameObject with the Damageable script attached and I know the collision script runs because the bolt destroys itself (the second line runs perfectly) on collision but the Debug.Log function isn't run.

If someone could tell me why the second If statement doesn't run and how I would make it work, I would greatly appreciate it.

Comment
Add comment · Show 4
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 GameVortex · Dec 17, 2013 at 08:24 AM 0
Share

Is the collider on the same GameObject as the Damageable component or on a child GameObject?

avatar image ArkaneX · Dec 17, 2013 at 08:43 AM 2
Share

As a side note - don't use GetComponent in if, only to call it again 2 lines later, in case when you hit correct object. Just use

 Damageable damageable = c.gameObject.GetComponent<Damageable>();
 if(damageable != null)
 {
     damageable.ApplyDamage(damage);
 }
avatar image Tpaley · Dec 17, 2013 at 11:22 PM 0
Share

Damagable is on the gameobject wih the collider.

avatar image OP_toss · Dec 18, 2013 at 12:13 AM 1
Share

Try using c.collider.gameObject.GetComponent()?

$$anonymous$$ight be that c.gameObject is the wrong gameObject in the collision. Print out its name so you know for sure. Otherwise, I'm at a loss, script looks fine save for @ArkaneX's suggestion.

$$anonymous$$ight I also suggest a coding convention? Damageable sounds like an interface type, like IDamangeable. You should either make it an interface, and implement it whenever you need something that is damageable, or rename it to something more specific/descriptive, without the "-able" suffix.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by salex100m · Dec 19, 2013 at 02:15 AM

right before your " if (c.gameObject.GetComponent() != null) " line, put this statement:

Debug.Log (c.gameObject);

what does it output as?

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 Tpaley · Dec 19, 2013 at 06:27 AM 0
Share

I already tried that. It outputs the gameobject wih the script on it. I'm starting to think that this is a Unity bug.

avatar image salex100m · Jan 05, 2014 at 03:58 PM 0
Share

Try destroying the object after a few seconds (like 5 seconds or something really long).

avatar image Tpaley · Jan 05, 2014 at 04:44 PM 0
Share

Which? The enemy or the weapon?

avatar image
0

Answer by murrayk012000 · Jul 25, 2017 at 08:15 PM

I'm hitting this same issue. Inside the IF statement the GetComponent method seems to return null. If I call GetComponent before the IF statement and compare against that then it works.

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

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

23 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

Related Questions

OnTriggerEnter issue 1 Answer

Player object still gets destroyed even when shields up 1 Answer

Creating a Switch that Controls a Gate 1 Answer

Score added on collision with another player 0 Answers

desrtoying on collision 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