First I used this script to load te level
function OnTriggerEnter (other : Collider) {
{
Application.LoadLevel(3);
}
But that didn’t work because it always loads the level.
Then I added the line:
If (other.gameObject.tag == “player”)
But then it doesn’t load the level at all.
Does anyone have tips on how to load the level when the monster touches the player?
make sure to reference the player’s collider, and make sure that the player tag is case-correct, the built in has a capital P
if(other.collider.gameObject.tag == "Player")
The reason why the first problem occur (loading the level all the time) is because the trigger function gets called every time the object, that the trigger collider belongs to, enters a trigger. Making the function gets called over and over again. Note also that trigger events are only sent if one of the colliders also has a rigidbody attached.
I guess this is something your script should look like. Sorry it’s written in C#, but basicly the same thing. I’m just afraid i’m gonna mess it up if i write it in javascript.
void OnTriggerEnter(Collider collision){
if(collision.gameObject.tag.Equals("Player")){
Application.LoadLevel(3);
}
}
Sources: Unity - Scripting API: Collider.OnTriggerEnter(Collider)