- Home /
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 );
}
}
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
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.
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.
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.
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
}
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.