Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Triggering animation, with Collider other and tags.

Discussion in 'Scripting' started by cristo, Oct 29, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    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.
    Code (CSharp):
    1.   gameObject.GetComponent<Animation>("OneFOL");
    2.   gameObject.PlayAnimation("OneFOL");

    Any help would be most appreciated.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FoliageScript : MonoBehaviour {
    5.     GameObject Foliage;
    6.     // Use this for initialization
    7.     void Start () {
    8.         Foliage = GameObject.Find("Foliage");
    9.      
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     public void OnTriggerEnter  (Collider other) {
    14.  
    15.         if (other.transform.tag == "OneFo")
    16.         {
    17.             gameObject.GetComponent<Animation>("OneFOL");
    18.             gameObject.PlayAnimation("OneFOL");
    19.         }
    20. }
    21. }
    22.  
     
  2. UltiGamer835

    UltiGamer835

    Joined:
    Jun 5, 2013
    Posts:
    8
    Try using this:

    Code (CSharp):
    1. //Instead of this
    2. gameObject.PlayAnimation("OneFOL");
    3.  
    4. //Try using this
    5. 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.
     
    cristo likes this.
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    No worries. Seems no matter what I try I get this error:

     
  4. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I actually got this working. I'm still such a noob. Apologies for the obvious error.