- Home /
2D Colliders not working
So I have the code to where the player is supposed to walking into the trigger of this object and it's supposed to bring up some gui text you can click on and answer, but Unity is not registering that the player is even colliding with the 2DBoxCollider. I need help because I'm at a stand still in my project and for the life of me I cannot figure this out, I have googled it and nothing works.
using UnityEngine;
using System.Collections;
public class ProtatoTalk : MonoBehaviour {
public string [] answerButton;
public string [] Questions;
bool DisplayDialog = false;
bool HasTalkedTo = false;
void onGUI()
{
if (DisplayDialog && !HasTalkedTo)
{
GUILayout.BeginArea (new Rect (1, 1, 400, 400));
GUILayout.Label(Questions[0]);
if(GUILayout.Button (answerButton[0]))
{
GUILayout.Label (Questions[1]);
HasTalkedTo = true;
}
if(GUILayout.Button (answerButton[1]))
{
GUILayout.Label (Questions[2]);
HasTalkedTo = true;
}
}
GUILayout.Label (Questions[3]);
GUILayout.EndArea();
}
void OnTriggerStay2D()
{
Debug.Log ("Player is in Trigger");
if(Input.GetMouseButtonDown(0))
DisplayDialog = true;
}
void OnTriggerExit2D()
{
DisplayDialog = false;
}
}
Your OnTrigger functions aren't complete, it should be:
void OnTriggerStay2D(Collider2D colObj)
void OnTriggerExit2D(Collider2D colObj)
Right now it's not looking for anything to enter the trigger.
Also, as the documentation says, a trigger will not trigger if neither object has a Rigidbody2D.
There's no need to include the Collider2D variable if you're not going to use it. This isn't the problem; just void OnTriggerStay2D()
etc. is fine for this code.
Ah weird, the documentation made it sound like it was needed. Good to know.
Answer by Eric5h5 · Feb 23, 2014 at 04:05 AM
There is no built-in function called "onGUI", so that function won't run unless you specifically call it. Note how in the code you posted, it didn't get syntax highlighted. This is why it's important that you be consistent about capitalizing all function names (and while you're at it, make all your variables start with lower case; don't mix cases like that).