Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RayneAvalon · Apr 16, 2013 at 04:48 AM · c#guitrigger

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RayneAvalon · Apr 18, 2013 at 08:00 PM 0
Share

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;
                 }
                 
             }
         
         }
     }
             
     
 
 }
 
avatar image LunaArgenteus · Apr 18, 2013 at 09:32 PM 0
Share

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).

avatar image RayneAvalon · Apr 19, 2013 at 04:51 AM 0
Share

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?

avatar image LunaArgenteus · Apr 19, 2013 at 10:30 AM 0
Share

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.

avatar image RayneAvalon · Apr 19, 2013 at 03:57 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges