Triggering animation, with Collider other and tags.

Hi, I’ve got a GameObject called Foliage with a collider in front of it. The collider is tagged OneFo. When the player collides with the tagged collider, I’d like some animation on the foliage to be triggered. The animation is called OneFOL.

This bit seems wrong. I’ve tried all kinds of variations of code I’ve used before to trigger animations that worked but I can’t find the problem.

  gameObject.GetComponent<Animation>("OneFOL");
  gameObject.PlayAnimation("OneFOL");

Any help would be most appreciated.

using UnityEngine;
using System.Collections;

public class FoliageScript : MonoBehaviour {
    GameObject Foliage;
    // Use this for initialization
    void Start () {
        Foliage = GameObject.Find("Foliage");
     
    }
   
    // Update is called once per frame
    public void OnTriggerEnter  (Collider other) {

        if (other.transform.tag == "OneFo")
        {
            gameObject.GetComponent<Animation>("OneFOL");
            gameObject.PlayAnimation("OneFOL");
        }
}
}

Try using this:

//Instead of this
gameObject.PlayAnimation("OneFOL");

//Try using this
gameObject.animation.Play("OneFOL");

Also, “gameObject” calls on the object the script is attached to, not the object that is being triggered, unless you already knew that and was activating an animation on the object this script is attached to.

And pardon me if the code is in correct, I don’t have time to check or test it since I’m currently in school.

1 Like

No worries. Seems no matter what I try I get this error:

I actually got this working. I’m still such a noob. Apologies for the obvious error.