- Home /
How to make my OnTriggerEnter() and Exit work
I have been working on this code with help from a different question for the past few days and i have gotten amazing results thanks to the people of this community. Now i reach out with this code again but with a different question. I have the OnTriggerEnter() and OnTriggerExit() functions coded accurately to my knowledge and all the required systems are set up so that when my player walks through a trigger the code activates, but when testing it that is not the case, can anyone help me figure out whats wrong with my code pelase?
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
public class KeyPad: MonoBehaviour {
//public variables
public Texture2D icon;
public string SetPassword = "12345";
public string PlayerPassword = "";
public bool KeyPadEnabled = false;
public bool DoorIsOpen = false;
public Transform target;
//trigger used to enable gui elements when player enters trigger zone
void OnTriggerEnter (Collider other) {
if(other.gameObject.tag=="Player")
{
KeyPadEnabled = true;
}
}
//trigger used to disable gui elements when player enters trigger zone
void OnTriggerExit (Collider other) {
if(other.gameObject.tag=="Player")
{
KeyPadEnabled = false;
if( DoorIsOpen == true)
{
target.Translate (Vector3.back *150* Time.deltaTime);
}
}
}
//gui function
void OnGUI () {
//check if gui elemnts should be shown.
if(KeyPadEnabled == true)
{
//display gui elemnts
GUI.Box (new Rect (10,10,300,300), new GUIContent(icon));
PlayerPassword = GUI.PasswordField (new Rect(60, 60, 200, 35), PlayerPassword, "*"[0],45);
//check for player input
if(Input.GetKeyUp(KeyCode.KeypadEnter))
{
//check passwords
if(PlayerPassword == SetPassword)
{
//translate targer and set control variable to true
target.Translate (Vector3.forward *150* Time.deltaTime);
DoorIsOpen = true;
}
}
}
}
}
again any and all help would be much appreciated. thank you
Answer by LunaArgenteus · Apr 16, 2013 at 06:11 AM
Have you tried putting Debug.Log statements in the OnTriggerEnter/Exit functions? If they fire, but it's just never identifying the object as the Player, then assuming you set everything up properly in the inspector, your problem is this:
Strings are considered objects, not primitives. Therefore, the == operator compares memory addresses, and not the value of the strings. Unless you are explicitly looking to see if one string object is the same as another string object (and not just whether the two strings hold equivalent values), you should be using .Equals() instead.
I see 3 places where you should replace this in your code.
Once each in OnTriggerExit/Enter
other.gameObject.tag.Equals("Player")
and once where you compare your passwords
PlayerPassword.Equals(SetPassword)
thank you for the tip luna, i have implemented your changes and through the debug.log statments i have found that my character controler is not setting off my trigger at all. an update for the code is listed below and i have my cube game objects mesh render turned or, IsTrigger turned on. any ideas?
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
public class $$anonymous$$eyPad: $$anonymous$$onoBehaviour {
//public variables
public Texture2D icon;
public string SetPassword = "12345";
public string PlayerPassword = "";
public bool $$anonymous$$eyPadEnabled = false;
public bool DoorIsOpen = false;
public Transform target;
//trigger used to enable gui elements when player enters trigger zone
void OnTriggerEnter (Collider other) {
Debug.Log ("Trigger Entered");
if(other.gameObject.tag.Equals ("Player"))
{
$$anonymous$$eyPadEnabled = true;
}
}
//trigger used to disable gui elements when player enters trigger zone
void OnTriggerExit (Collider other) {
Debug.Log ("Trigger Exited");
if(other.gameObject.tag.Equals("Player"))
{
$$anonymous$$eyPadEnabled = false;
if( DoorIsOpen == true)
{
target.Translate (Vector3.back *150* Time.deltaTime);
}
}
}
//gui function
void OnGUI () {
//check if gui elemnts should be shown.
if($$anonymous$$eyPadEnabled == true)
{
//display gui elemnts
GUI.Box (new Rect (10,10,300,300), new GUIContent(icon));
PlayerPassword = GUI.PasswordField (new Rect(60, 60, 200, 35), PlayerPassword, "*"[0],45);
//check for player input
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.$$anonymous$$eypadEnter))
{
//check passwords
if(PlayerPassword.Equals (SetPassword))
{
//translate targer and set control variable to true
target.Translate (Vector3.forward *150* Time.deltaTime);
DoorIsOpen = true;
}
}
}
}
}
Hmm, I would double check your scene to make sure that your $$anonymous$$eyPad script is attached to the same GameObject as your trigger, and not a child/parent of it.
If that doesn't work, I would double check the assigned layers of your player and trigger objects and make sure that they didn't somehow end up on layers that are not set to interact in the Physics inspector (to see your collision matrix, Edit->Project Settings ->Physics).
i double checked and i had my kepad script on the camera of my character controller and not the controler facepalm. i moved it and now its setting off the debug logs but my script still isnt working. when it enters the trigger the gui is supposed to pop up but it doesn't and i have no clue why. everything compiles, and yet i still cant seem to get this figured out. have i coded something wrong or misnamed a variable or something?
Perhaps it's a typo, but you say "i had my kepad script on the camera of my character controller and not the controler." Does this mean you now have the keypad script on your player, but it was previously on the camera?
Say you have a $$anonymous$$eypad in your game world - this is where your trigger volume should be, and this trigger volume is what the $$anonymous$$eyPad script should be attached to. The player itself should not be a trigger and should not have the $$anonymous$$eyPad script attached to it. If you currently have your $$anonymous$$eypad script attached to the player, of course you'll never see the gui - the script is expecting the OTHER object to have the "Player" tag.
facepalm again i see, i hadn't realized that. every time i have tried working with GUI elements before i have needed to attach the script to the camera to get it to work. Thank you for all the help.
Your answer
Follow this Question
Related Questions
Help with making a triggered gui message 1 Answer
Distribute terrain in zones 3 Answers
UI button doesn't appear - c# 3 Answers
gui label works in js but not in cs 1 Answer
Multiple Cars not working 1 Answer