- Home /
gameObject not detecting collision
Okay, so I got my player controller colliding with an object that I want to pick up, and it does collide with it, but in the pick up item's script it is set to delete itself upon collision and does not, and with a Debug.Log I found out that even though the player is colliding with it physically, it isn't... not sure how to say it but it doesn't output anything when I type Debug.Log(collider.gameObject.tag); when the player is tagged as Player. And I know the debug.log works because the bullets I shoot at it output an "Untagged" in the console.
I'll reference some code:
function Start()
{
//Physics.IgnoreLayerCollision(10, 11, true);
//Physics.IgnoreLayerCollision(9, 11, true); //commented out for debugging
//Physics.IgnoreLayerCollision(11, 11, true);
}
function OnCollisionEnter(collision : Collision)
{
Debug.Log(collision.gameObject.tag); //<--- this outputs nothing when the player hits it
if (collision.gameObject.tag != "Player")
return;
Inventory.AddItem(item);
Destroy(gameObject);
}
I also tried making it collide with other random objects and they also don't seem to pass a .tag value, so it seems the only thing that can collide with this object is a bullet.
Edit for statement:
function OnControllerColliderHit (hit : ControllerColliderHit)
{
var body : Rigidbody = hit.collider.attachedRigidbody;
Debug.Log( hit );
Debug.Log( body );
}
Both debugged nothing to the log.
You sure your scripts are on the same object that has your character controller and collider (and not a child/parent of it)? If you are unsure, you can try this easily by doing function Start() { print(" I have " + GetComponent(CharacterController) ); }
Answer by Melang · Aug 21, 2014 at 06:23 AM
Make sure at least one of the colliding objects has a Rigidbody attached.
If your colliders and rigidbodies are 2d, the function should be called
function OnCollisionEnter2D(coll: Collision2D)
If it still doesn't work, add a
Debug.Log("on collision enter");
to see if it even triggers.
Answer by Statement · Apr 03, 2011 at 12:45 AM
Sorry for short reply - going bed - but check out OnControllerColliderHit instead of OnCollisionEnter.
Alternatively (unsure if it helps) you could try adding a rigidbody to the collider and set is$$anonymous$$inematic to true.
Alright, checking both of those. I added to my main post the code I added with oncontrollercolliderhit which didn't seem to work, although I'm still trying new things with it now.
Answer by zak666 · Aug 21, 2014 at 04:36 AM
Still dose nothing and yet no script errors sure was a good try
#pragma strict
function OnTriggerEnter (other:Collider){
if(other.gameObject.name == "PlayerHyperShot1"){
Debug.Log("It went through");
ProbeEvent.ProbeScore += -1;
_MASTER.Score += 200;
Destroy(gameObject.Find("PlayerHyperShot1"));
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613120425im_/https://answers.unity.com/themes/thub/images/avi.jpg)