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 TheFrankman123 · May 02, 2012 at 04:08 PM · ontriggerenteractiveontriggerexit

Trigger Enter Issue

I have a trigger enter that is working bizarrely. The trigger enter is looking to see if the player has entered it and if it does turn a game object on and then off on exit. However, when i start the game it automatically thinks that the player has entered the trigger despite the player being really far away from the trigger. I am good with Unity and scripting, used trigger enter for a lot of things without issue. Here is my code, if anyone can shed some light on this problem that'd be brilliant.

 #pragma strict
 var securityCameraHolder : GameObject;
 var soundActive : boolean = false;
 
 function Start () {
     soundActive = false;
 }    
 
 
 function OnTriggerEnter (playerEnter : Collider) {
     if(playerEnter.gameObject.name == "player");    
         soundActive = true;
         Debug.Log("TriggerEntered");
 }
 
 function OnTriggerExit (playerExit : Collider) {
     if(playerExit.gameObject.name == "player");
     soundActive = false;
     Debug.Log("TriggerExit");
 }
 
 function Update () {
     
     if(soundActive == true) {
         securityCameraHolder.active = true;    
         Debug.Log("Security Camera is true");
     }
      
     if(soundActive == false) {
         securityCameraHolder.active = false;
         Debug.Log("security camera is false");    
     }
     
 }
Comment
Add comment · Show 2
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 Noah-1 · May 02, 2012 at 04:27 PM 0
Share

I could not find anything wrong with your code, But try using OnTriggerStay ins$$anonymous$$d of OnTriggerEnter and try tagging the objects ins$$anonymous$$d of calling them by their name, maybe it works that way.

avatar image AlucardJay · May 02, 2012 at 05:35 PM 0
Share

need more info on the console outputs you are getting.

Also I notice you have a ; after the condition of your if statement in the trigger functions i.e.

 if(playerExit.gameObject.name == "player"); <== ???? remove ;
 
 
 

also , try adding more info to the trigger debugs just to check if it is the playerObject that is triggering the event , i.e.

 Debug.Log("TriggerEntered by " + playerEnter.gameObject.name);
 
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by venkspower · May 02, 2012 at 05:23 PM

Your script is absolutely right. Since you have initialized the value of soundActive to false, and have given if(soundActive == false) in the Update, there will be printing "security camera is false" every frame. That is causing the problem.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Problem using trigger to set gameobjects active 1 Answer

Problems with OnTriggerEnter and OnTriggerExit 2 Answers

OnTriggerEnter function in c# 0 Answers

Make object react to certain triggers only 1 Answer

Finding and assigning Target in radius (with colliders) 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