- Home /
Multiple OnTriggerEnter on one script?
Hello, i have planets on my game and if the character (with character controller) touches them something will happen for each planet my first try was to code each planet with ontriggerenter and write down the code after it but once i write another planets code after it gives me an error message saying "already has a definition for 'OnTriggerEnter(UnityEngine.Collider)'." here is my code
//planet 1
function OnTriggerEnter (Planet_Earth_Entry : Collider) {
Application.LoadLevel("Planet_Earth");
}
//Planet 2
function OnTriggerEnter (Planet_Mars : Collider) {
//destroy ship
destroy.gameObject.FindWithTag("Main_Character");
}
next since that didnt work i tried "oncollisionenter" but i couldn't even get that to work no errors just didn't work :S here is that code which i was just testing
function OnCollisionEnter(theCollision : Collision)
{
Debug.Log("Hit planet");
}
now my question is can i put multiple OnTriggerEnter's on one script? because the only other way i can think to do it is by having a lot of scrips one for each planet and my plan for my game i would end up having 100 scripts... please help thank you
Answer by aldonaletto · Oct 05, 2011 at 11:42 PM
You can't, but you can put the OnTriggerEnter in the player's script (trigger events are sent to both, the trigger and the object the entered it) and compare the planet name:
function OnTriggerEnter(col: Collider){ if (col.name == "Earth"){ Application.LoadLevel("Planet_Earth"); } else if (col.name == "Mars"){ Destroy(gameObject); // player suicides if planet is Mars } else if (col.name == "Venus"){ ....
Hey, sorry i didn't reply quicker thank you! it worked
This answer my question in link text So im giving this a thumbs up =D(Even thought this is old)