OnCollisionEnter is not working

Hi,

Here is my pickupScript:

var Gun : GameObject;
var Bullet : GameObject;

function OnCollisionEnter(collision : Collision)
{
	if (Gun.gameObject.tag == "gun");
		audio.PlayOneShot(pickupGunSound);
		carryingGun = true;
		Debug.Log( "YOU HAVE THE GUN" );
		//Destroy(Gun.gameObject);

	if (Bullet.gameObject.tag == "bullet")
		audio.PlayOneShot(pickupBulletSound);
		carryingBullet = true;
		Debug.Log( "YOU HAVE THE BULLET" );
		//Destroy(Bullet.gameObject);
}

But when I enter Game Mode, as soon as the player controller touch the ground I get both DebugLog messages: "YOU HAVE THE BULLET"and “YOU HAVE THE GUN”.
Tags are set on each objects, as well as rigidbodies and colliders.
What am I missing ?
Please help.

HI Kaha

You add these scripts on respective objects GUN and bullet but you want the message when your player touch any one of the object , right ?

using this you doesn’t need multiple scripts to check it player touched gun / bullet or not…
if you want like this you need to put these both code in a same file and assign script to your player object like this code

function OnCollisionEnter(collision : Collision)
{
    if (collision.gameObject.tag == "gun"){
       audio.PlayOneShot(pickupGunSound);
       ReloadScript.carryingGun = true;
       Debug.Log( "YOU HAVE THE GUN" );
       //Destroy(Gun.gameObject);
}
       if (collision.gameObject.tag == "bullet"){
       audio.PlayOneShot(pickupBulletSound);
       ReloadScript.carryingBullet = true;
       Debug.Log( "YOU HAVE THE BULLET" );
       //Destroy(Bullet.gameObject);
}
}

But keep in you mind if you are using CharacterController you you can’t access OnCollisionEnter Event of the Physic you require another method named
OnControllerColliderHit

Gun has a tag “gun” and Bullet has a tag “bullet”, right? So, both conditional will always be true. What you probably want to do is:

function OnCollisionEnter(collision : Collision)
{
    if (collision.gameObject.tag == "gun"){
       audio.PlayOneShot(pickupGunSound);
       carryingGun = true;
       Debug.Log( "YOU HAVE THE GUN" );
       //Destroy(Gun.gameObject);
    }
    if (collision.gameObject.tag == "bullet"){
       audio.PlayOneShot(pickupBulletSound);
       carryingBullet = true;
       Debug.Log( "YOU HAVE THE BULLET" );
       //Destroy(Bullet.gameObject);
    }
}

The collision object stores the objects with the collision occurred, so, to test which object is that, you sholud use collision.gameObject.