- Home /
OnTrigger not working in 3DPlatformer Tutorial
In the following script, I have the player, Lerpz running into a sphere object, "QuestionMark". I have Debug.Log right after to see if ontrigger works, however am not getting an output. Would appreciate any direction. There is a character controller on Lerpz, however no rigidbody on the sphere.
var questiontrigger : boolean = false; private var mover : DroppableMover; var playerStatus : ThirdPersonStatus;
function Start () { // do we exist in the level or are we instantiated by an enemy dying? mover = GetComponent(DroppableMover); }
function OnTriggerEnter (col: Collider) { if(mover && mover.enabled) return; var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus); Debug.Log("YES"); if(col.gameObject.name == "QuestionMark") questiontrigger = true; Destroy(this); }
//function OnTriggerExit (col: Collider) { // if(col.gameObject.name == "QuestionMark") { // questiontrigger = false; // } //}
function OnGUI () { if(questiontrigger) { GUILayout.Label("Here are your instructions for level 1");
}
}
Answer by MC HALO · Apr 17, 2011 at 11:23 AM
it is not name it is tag so it should be:
var questiontrigger : boolean = false;
private var mover : DroppableMover;
var playerStatus : ThirdPersonStatus;
var QuestionMark: GameObject;
function Start () { // do we exist in the level or are we instantiated by an enemy dying?
mover = GetComponent(DroppableMover); }
function OnTriggerEnter (col: Collider) {
if(mover && mover.enabled) return;
var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
Debug.Log("YES");
if(col.gameObject.tag == "QuestionMark") questiontrigger = true;
}
function OnTriggerExit (col: Collider) {
if(col.gameObject.tag == "QuestionMark") {
questiontrigger = false;
Destroy(QuestionMark );
}
}
function OnGUI () {
if(questiontrigger) {
GUILayout.Label("Here are your instructions for level 1");
}
}
i think the line :
Destroy(this)
was the problem because that means you are telling the script to destroy itself and then the gui function along with that destroys itself same with the OnTriggerExit. so what i add was
Destroy(QuestionMark);
in the on trigger exit function now that will destroy the object that you collide with once it has been destroyed. and then the GUI will boolean will be set to false and etc. but make sure this script is attached to the object not the player. and the reason i changed:
if(col.gameObject.tag == "QuestionMark")
to:
if(col.gameObject.tag == "Player")
is because no we are telling the script that if the player enters the object then do the collider if he leaves the collider destroy the object he collides with.
i also created a var on top which is called :
var QuestionMark: GameObject;
now this is important once you add the script to the collider object not the player a variable will show up in the inspector mode called :
QuestionMark
now this will be asking for the object that it needs to destroy so drag the collider object in the variable and that should be it. Don't forget not the player :)
hope this helps :)