- Home /
Question by
ina · Dec 26, 2011 at 11:40 PM ·
collisiongameobjectgetcomponentcomponent
Detecting if other GameObject collided with has a certain component
How do you detect if the other gameobject you collide with has a certain component (before attempting to GetComponent)?
Comment
Answer by aldonaletto · Dec 26, 2011 at 11:52 PM
If the "certain component" is a rigidbody, you can test ControllerColliderHit.rigidbody (or Collision.rigidbody, if your object isn't a CharacterController):
function OnControllerColliderHit(hit: ControllerColliderHit){
if (hit.rigidbody){
// object hit has a rigidbody
}
}
Other components can only be checked using GetComponent:
function OnControllerColliderHit(hit: ControllerColliderHit){
var comp: CertainComponent = hit.transform.GetComponent(CertainComponent);
if (comp){
// object hit has the component
}
}
Your answer
Follow this Question
Related Questions
AddComponent for RawImage not functioning as expected 0 Answers
GetComponent of ALL clones? 2 Answers
Getting Another Object's ID with Colliders 1 Answer
Weird GetComponent error 2 Answers