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 WillB · Apr 30, 2013 at 05:05 PM · collisioncharactercontrolleroncollisionenteroncontrollercolliderhit

OnControllerColliderHit -> GetComponent on collided object not working

I have been researching this for 6 hours. Slept on it, another 2 hours and still no luck. I have never felt so defeated; this problem has to be something so simple and stupid! Here's the skinny:

I have a player GO that is a modified CharacterController ("Player"). It is the parent of a small sphere GO that has a SphereCollider on it ("Weapon"). Any time I try to collect the Enemy script from collisions, I pop 3 generic errors.

I am testing collisions against a Cube primitive that has a BoxCollider and a Rigidbody ("Enemy"). It also contains a script (Enemy.cs) with the basic badguy functions.

Codewise on the Player (parent, CharacterController):

  private Enemy enemy;
  void OnControllerColliderHit(ControllerColliderHit hit) {
         if (hit.gameObject.tag == "Enemy") {
             enemy = hit.gameObject.GetComponent(Enemy);
             enemy.TakeDamage(25);
         }
      }

I have also tried:

 void OnControllerColliderHit(ControllerColliderHit hit) {
        hit.gameObject.GetComponent(Enemy).TakeDamage(25);
      }

Throws these errors on the line where I try to GetComponent():

  • Error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

  • Error CS1502: The best overloaded method match for UnityEngine.GameObject.GetComponent(System.Type)' has some invalid arguments - **Error CS1503:** Argument #1' cannot convert object' expression to type System.Type'

I get the same errors also trying OnCollisionEnter() attached to the Weapon GO. I learned that the Parent GO has to handle collisions for its children, which led to dropping the OnControllerColliderHit() onto the Player.

Thanks for taking the time to read this. I appreciate any help you can offer. I'm thinking about just not worrying about collisions between the Player and anyone else, un-childing (bastarding?) the Weapon GO and then just enabling it around the player during its animation so it can handle its own collisions.

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 Loius · Apr 30, 2013 at 05:14 PM

You need to GetComponent(typeof(Type)). It's weird, yes.

You can also GetComponent.

In unityscript, GetComponent(Type) works fine because it's overall a very lazy language. I mean you don't even have to explicitly cast every single set of digits to float! xD

Comment
Add comment · Show 1 · 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 WillB · Apr 30, 2013 at 05:19 PM 0
Share

If you were here right now I'd hug you; thanks!!

The stupid thing? I was so tunnel-visioned that I didn't even bother checking the ScriptReference for GetComponent....I even have correct GetComponents in other parts of the demo.... hahah

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

13 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

Related Questions

Object won't collide with FPS Controller 2 Answers

Collision with 2 CharacterControllers not precise 1 Answer

CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers

Character Controller OnControllerColliderHit never called. 0 Answers

Character Controller problem 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