- Home /
How to destroy and trigger on enter.
So I'm working on a game and it has a properly operating currency system. I am trying to make a trigger destroy when the player enters with a certain amount of coins. I have made a static var called currentScore and when the player reaches 2 coins I want the field to destroy so that the player can pass through and its not working
script:
#pragma strict
function OnTriggerEnter(other : Collider) {
if (currentScore == "2") {
Destroy.gameObject;
}
}
The script is bound to the trigger. Any help would be greatly appreciated considering I'm a noob.
Cheers
Answer by BenKurdziel · Oct 04, 2014 at 05:23 AM
Try:
Destroy(this.gameObject);
I think that would work, but I'm getting the error: Unkown identifier 'currentScore'.
If currentScore exists in a different script, you need to reference that script first.
For example, if currentScore is an int defined in a script called "Game$$anonymous$$anager" that you dragged onto a GameObject in the scene called "Game$$anonymous$$anagerObject", then you would need to do the following in this script.
var gm : Game$$anonymous$$anager;
function Start ()
{
gm = GameObject.Find("Game$$anonymous$$anagerObject").GetComponent(Game$$anonymous$$anager);
}
function OnTriggerEnter (other : Collider)
{
if (gm.currentScore == 2)
{
Destroy(this.gameObject);
}
}
I'm a little rusty with javascript, but I think this is the correct syntax.