- Home /
Boolean detection on collision.
I'm not sure what I'm doing wrong but I have a simple script which sets boolean true or false depending on whether the collider is entered or not. I'm trying to make it bring up a debug log before I add the sound effect and counter for walking.
The collider is set to "Is Trigger", so I'm not sure if there is something else.
#pragma strict
var GrassBool : boolean = false;
function OnTriggerEnter (Grass : Collider)
{
if(Grass.gameObject.tag=="Player")
{
GrassBool = true;
}
}
function OnTriggerExit (Grass : Collider)
{
if(Grass.gameObject.tag=="Player")
{
GrassBool = false;
}
}
function Update ()
{
if(Input.GetKey(KeyCode.W) && GrassBool == true)
{
Debug.LogWarning("You are on grass");
}
}
Did you add this script to the player or the Grass collider? If you added it to the player then checking if the collider tag is "player" won't make any sense :) Here's a simple way to see what colliders you are hitting - add this debug:
Debug.Log("Hit object with tag: "+Grass.gameObject.tag)
just after this line:
function OnTriggerEnter (Grass : Collider) {
Thanks for your time. I already had the script on the collider, as you say wouldn't work otherwise.
I added the debug and it brought back the tag as "Untagged". I'm 100% sure my FPS controller is tagged "Player".
Unless something which is a child is untagged...
Perhaps try this ins$$anonymous$$d to see what the object name is:
Debug.Log("Hit object named: "+Grass.gameObject.name)
I didn't realise you could debug that way. Seems like it's finding a gameObject that isn't used anymore. I disable the object and the collider doesn't pass any debug. I swear there is a different problem every time I try something like this -.-
Answer by MileSplit · Mar 13, 2013 at 09:40 PM
Hello, This gave me quite the headache. You need to have a rigidbody attached to one of the objects.
I thought that myself but added it to either object and makes no difference to what happens. Still throws back "Untagged".
Your answer
Follow this Question
Related Questions
Collision with renderer.enabled? 0 Answers
make script target player that enters collider 0 Answers
Collider not working... 0 Answers
Trigger Enter and Exit not working properly? 2 Answers
Boolean wont acitvate if the enemy enters a trigger 1 Answer