- Home /
Animated rule tile with trigger event
Ive created a tall clump of grass which shifts like someone disturbed it. what I want is to have it in idle mode then trigger it to animate when someone triggers its collider. I set this up as a prefab and it works when i put together a bunch of prefabs yet i want to put this prefab into a rule tile to use it in a tile palette. So far ive gotten to the point of absolutely nothing working so thats to say im at square 0 on this one.
the collider2D is on the prefab as well as the script which tells the controller to trigger the animation. When i add a tilecollider to the grid i can see the collider2d on the prefabs but they dont do anything
my script
public class movegrass : MonoBehaviour
{
private Animator myAnim;
private void Start()
{
myAnim = gameObject.GetComponent<Animator>();
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.name == "Player")
{
myAnim.SetTrigger("Grass");
}
}
}
Answer by BBIT-SOLUTIONS · Mar 22, 2020 at 10:43 PM
You should compare strings with Equals()
instead of ==
So line 11 should be
if (collision.name.Equals("Player"))
== works just fine in this case and changing to .Equals did nothing for my problem.
Is the Collider2D component on your prefab marked as isTrigger
(in Inspector)?
Yup. Like i said when its a prefab it works just fine. Its only when i convert the prefab to a tile object that the script stops working
ok, i see. Are the colliders of the single GameObjects maybe overlapping each other? So that they are constantly triggered by each other and not "open" for collisions with the player?
If yes, you could maybe try to make the colliders a bit smaller.
Your answer
![](https://koobas.hobune.stream/wayback/20220612225522im_/https://answers.unity.com/themes/thub/images/avi.jpg)