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 urasimakei · May 06, 2014 at 01:29 PM · triggerfunction

I can't trigger OnTirggerExit2D(Collider2D)!!

I want to use trigger function. but i can't.

Ironically, onTriggerEnter2D( collider2d ) and onTriggerStay2D( collider2d ) are working!

this is my code. i want to control by collider's position.

 using UnityEngine;
 using System.Collections;
 
 public class ButtonUI : MonoBehaviour
 {
     public Vector2 resolution = new Vector2( UI.DEFALUT_SCREEN_WIDTH, UI.DEFALUT_SCREEN_HEIGHT );
     public GUIStyle style;
     public GUIContent content;
     public Vector2 position;
     public Vector2 size;
     Rect rect;
 
     bool collisionState = false;
     string message;
 
     public BoxCollider2D collider;
 
     void Start ()
     {
         this.rigidbody2D.gravityScale = 0.0f;
 
         rect = UI.getAbsoluteUIRect( position, size, resolution );
         collider.size = size;
         collider.center = rect.center;
     }
 
     void Update ()
     {    
     }
 
     void OnGUI()
     {
         GUI.Button( rect, content, style );
 
         if( collisionState == true )
         {
             GUI.Box( rect, message );
         }
     }
 
 
     void OnTriggerStay2D( Collider2D collider )
     {
         if( collider.gameObject.tag == "UI" )
         {
             collisionState = true;
             message = "stay";
             print( message );
         }
     }
 
     void OnTriggerExit2D( Collider2D collider )
     {
         if( collider.gameObject.tag == "UI" )
         {
             collisionState = false;
             message = "exit";
             print( message );
         }
     }
 
     void OnTriggerEnter2D( Collider2D collider )
     {
         if( collider.gameObject.tag == "UI" )
         {
             collisionState = true;
             message = "enter";
             print( message );
         }
     }
 }
 
 public class HandUI : MonoBehaviour
 {
     public GUIStyle style;
     public GUIContent content;
     public float radius;
     Rect rect;
 
     public CircleCollider2D collider;
 
     void Start ()
     {
         collider.radius = radius;
         collider.center = rect.center;    
     }
 
     void Update ()
     {
         rect = new Rect( Input.mousePosition.x - 50, Screen.height - Input.mousePosition.y - 50, radius * 2, radius * 2 );
 
         collider.center = rect.center;
     }
 
     void OnGUI()
     {
         GUI.Button( rect, content, style );
     }
 }
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 andyspeak · May 06, 2014 at 01:46 PM 0
Share

your code is good i think so the problem is on what happends to the object on enter if you destroy it it wont leave lmao

what does "ui" stand for and is ther any scripts attached that cud be the prob

avatar image urasimakei · May 06, 2014 at 04:55 PM 0
Share

thx.. but.. i can't understand.. i just want to print message. and that is all of my code. UI class just contains static function and calculate resolution. just two classes are used.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sprawl · May 06, 2014 at 05:24 PM

Are you sure the object is leaving the trigger or are you deleting it ?

If you are deleting the object instead of moving it away, OnExit won't trigger since you won't be able to find the tag of a destroyed object.

Comment
Add comment · Show 1 · 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 urasimakei · May 07, 2014 at 09:53 AM 0
Share

thx for the answer. but I'm not deleting the object. i deleted "if statement", but It's still not working. void OnTriggerExit2D( Collider2D collider ) { collisionState = false; message = "exit"; print( message ); }

 void OnTriggerExit2D( )
 {
     collisionState = false;
     message = "exit";
     print( message );
 }

these codes also doesn't work.

avatar image
0

Answer by EvilTak · May 07, 2014 at 03:01 AM

Check if is trigger of your box collider is true. If it is, then check if your trigger or other collider has a rigid body. According to Unity Script reference, either the trigger or the other collider should have a rigidbody. If you are planning to make UIs with triggers, it will be better to use:

 RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.MousePosition), Vector2.zero);
 if(hit != null)
 {
     //your code here
 }
Comment
Add comment · Show 1 · 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 urasimakei · May 07, 2014 at 10:05 AM 0
Share

thx for the answer. I had already checked your advise. but It had not worked.

I just want to control UI by $$anonymous$$essage Function. If I want to use polling, I wouldn't do this. and I want to control "Circle and Rectangle Crash". not "Point and Rectangle Crash" or "Point to Circle Crash". I might make this algorithm. but I want to use Unity function.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have OnGUI element in a OnTriggerEnter function? 3 Answers

Can't click gameobject when over another trigger? 1 Answer

OnTriggerEnter can't detect my Transform tag 1 Answer

Running a function on something in an area. 1 Answer

Negating a function. 2 Answers


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