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 Bonifatius GK · Dec 04, 2012 at 10:33 PM · c#errorgetcomponentcast

C# getComponent result error cast

this is a line of code to get a component in children, every time i use this "getComponent", "getComponentInChildren", etc. there is an error always come up "cannot convert type 'UnityEngine.Component' to 'UnityEngine.ControllerColliderHit'" or else up to the type. i don't understand why, and as long as i see, it's very easy to do on JS than on C# even with a force cast..

 ControllerColliderHit hitObject = (ControllerColliderHit)transform.FindChild("EnemySight").GetComponent(typeof(ControllerColliderHit))

what i know if this getComponent function suppoe to return as the type. anyone knows why..?? thx.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eric5h5 · Dec 04, 2012 at 10:41 PM

You need to cast it properly; you're doing the casting in the wrong place:

 hitObject = transform.FindChild("EnemySight").GetComponent(typeof(ControllerColliderHit)) as ControllerColliderHit

Or else use the generic version:

 hitObject = transform.FindChild("EnemySight").GetComponent<ControllerColliderHit>()

The non-generic GetComponent returns the type of the component rather than Component in Unityscript (as long as you don't use a string), but not in C#.

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
avatar image
0

Answer by bompi88 · Dec 04, 2012 at 10:43 PM

About a year without Unity, but I think I remember that you could do something like this:

 ControllerColliderHit hitObject;
 hitObject = transform.FindChild("EnemySight").GetComponent<ControllerColliderHit>();
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
avatar image
0

Answer by Bunny83 · Dec 05, 2012 at 12:08 AM

ControllerColliderHit is a struct and is not derived from Component. It's only use is as parameter for the OnControllerColliderHit callback.

This cast is not possible since ControllerColliderHit is something totally different as a Component. I'm not sure what you actually want to do, i guess you want to access the CharacterController (which is a Component).

I suggest you take a look at the inheritance tree to see which class is derived from what.

Comment
Add comment · Show 4 · 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 Bonifatius GK · Dec 05, 2012 at 07:22 AM 0
Share

actually, i need to know when the child collided to certain collider. how do i suppose to do then..??

avatar image Bunny83 · Dec 05, 2012 at 07:54 AM 0
Share

A child? Of a charactercontroller? That doesn't work since the Charactercontroller don't uses physics. It only checks it's capsule collider. Use a Rigidbody ins$$anonymous$$d. $$anonymous$$eep in $$anonymous$$d that a Rigidbody works a bit different than a CC.

avatar image Bonifatius GK · Dec 05, 2012 at 09:17 AM 0
Share

a child of a game object (gameObject inside gameObject) which has it's own collider

avatar image Bunny83 · Dec 05, 2012 at 09:33 AM 0
Share

yes, as i said it doesn't work with a CharacterController. You have to use a Rigidbody to detect collisions with other colliders. The CC can only use it's own capsule.

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

12 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

Related Questions

Distribute terrain in zones 3 Answers

Problem when Getting a component from a non Mono Behavior class. 1 Answer

Problem calling variable across scripts (Closed) 1 Answer

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 1 Answer

Float to int casting in C# script with modulo giving division by zero error 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