- Home /
unity 2d create an ammo box
Hello everyone.
I have a problem filling this script. This script is associated boxcollider2D with a trigger is true. But when my character enters the boxcollider2D not grant me the grenades and the box is not destroyed. Some Suggested?
Script.
#pragma strict
static var GRENADE_AMMO = 0;
static var temp : int = 5;
function OnTriggerEnter2D (coll : BoxCollider2D)
{
if(coll.gameObject.tag=="crateGrenades")
{
Destroy(coll.gameObject);
GRENADE_AMMO += 4;
print("YOU NOW HAVE "+ GRENADE_AMMO +" GRENADES");
GameObject.Find("g_Count").guiText.text = ""+GRENADE_AMMO;
}
}
Answer by mwbranna · May 04, 2015 at 06:50 PM
Your function override has to match exactly for Unity to recognize it. You are using BoxCollider2D instead of Collider2D. Use this instead:
function OnTriggerEnter2D(coll: Collider2D)
Also make sure there is a rigidbody attached to the gameObject and that the collider has the Is Trigger box checked. You can add Debug.Log() statements inside the function (outside of the "if"s) to make sure the function is getting called.
I modified the script in this way. Do you have any $$anonymous$$gested for addattarlo a boxcollider2D?
pragma strict
static var GRENADE_A$$anonymous$$$$anonymous$$O = 0;
static var temp : int = 5;
var Sound : AudioClip;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "crateGrenades")
{
Destroy(hit.gameObject);
gameObject.audio.PlayOneShot(Sound);
GRENADE_A$$anonymous$$$$anonymous$$O += 4;
print("YOU NOW HAVE "+ GRENADE_A$$anonymous$$$$anonymous$$O +" GRENADES");
GameObject.Find("g_Count").guiText.text = ""+GRENADE_A$$anonymous$$$$anonymous$$O;
}
}
Answer by safak93 · May 05, 2015 at 07:20 AM
Try this:
#pragma strict
static var GRENADE_AMMO = 0;
static var temp : int = 5;
var Sound : AudioClip;
function OnTriggerEnter2D(hit: Collider2D)
{
if(hit.gameObject.tag == "crateGrenades")
{
gameObject.audio.PlayOneShot(Sound);
GRENADE_AMMO += 4;
print("YOU NOW HAVE "+ GRENADE_AMMO +" GRENADES");
GameObject.Find("g_Count").guiText.text = ""+GRENADE_AMMO;
Destroy(hit.gameObject);
}
}
Your answer