Why isn't my BoxCollider2D doing anything?
I'm trying to make a speech bubble appear when you approach the tutorial board.
The bubble is there and it has its animation states "Appear" and "Disappear"
Appear is called OnTriggerEnter2D
Disappear is called OnTriggerExit2D
The collider is on the parent, tutorial board
The animator and script is on the bubble itself Can't add more images due to rules.
This is the script I have attached to the bubble, the conditions script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Conditions : MonoBehaviour
{
private Animator bubblecondition;
private BoxCollider2D boxCollider;
// Start is called before the first frame update
private void Awake()
{
bubblecondition = this.GetComponent<Animator>();
bubblecondition.Play("Disappear", 0, 1f);
}
void Start()
{
//bubblecondition.SetBool("Here", false);
boxCollider = this.GetComponentInParent<BoxCollider2D>();
}
// Update is called once per frame
void Update()
{
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
bubblecondition.Play("Appear", 0, 0f);
}
else
{
return;
}
}
public void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
bubblecondition.Play("Disappear", 0, 0f);
}
else
{
return;
}
}
}
What am I doing wrong? I spent an hour trying to know why. I even tried to have the console write a message on collision but it's just dead. Help me see my mistake :)
Also, before you ask, every other item (collectible) in the game works well. The player has a rigidbody component. Everything else that has a collider is working well.
It's just this one collider.
Answer by AHAKuo · Mar 13, 2020 at 02:47 PM
SOLVED
I simply put the conditions script on the board itself and referenced the collider as private BoxCollider2D directly. This fixed it oddly. Don't know exactly why that was happening.
you beat me to it! Trigger finger is faster than the submit finger ;-)
lmao. All good. Actually, your comment helped me understand it. $$anonymous$$y fix was arbitrary and I didn't even know why it worked xD
Answer by streeetwalker · Mar 13, 2020 at 03:00 PM
Hi @AHAKuo, It looks like that If this script is on the Bubble, trigger is never going to fire because the object with the trigger collider is it's parent object.
Not sure what you are trying to do with this:
boxCollider = this.GetComponentInParent();
but that does not make the parent object own the OnTrigger statements in this script. I'm guessing bubble does not have a trigger collider, nor would it work because (I gather) it's inactive.
Put this script on the Tutorial Board. Then OnTrigger will catch the collision events.
Thank you so much for making me understand it! It makes sense now. God, I must've spent 2 hours actually. Waste of time
Thanks a lot. This is new information for me ^^