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.
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”