- Home /
Animation on public transform from script 2D
Hi,
I need some help with the following script. The idea was that I could have a public transform with an animation component (I removed the animator from the object, I don't know if that's what I want to do). This way I can attach the script I wrote to a box collider 2d and if the player enters it, then the animation on whichever object I place will play.
using UnityEngine;
using System.Collections;
public class AnimationTrigger : MonoBehaviour {
public Transform animatedObject;
private CharacterController2D _controller;
public void Awake(){
Debug.Log(animatedObject);
_controller = GetComponent<CharacterController2D> ();
}
public void OnTriggerEnter2D(Collider2D col){
Debug.Log ("Player has triggered the animation!");
if (_controller != null) {
animatedObject.animation.Play();
}
gameObject.SetActive (false);
}
}
So that is the script I wrote. It recognizes the object to be triggered and the player, but if I debug the animation to play it says null and nothing happens when I move through the collider other then the console telling me that I did so.
Is what I am attempting not possible or am I missing something?
I am just learning so any help would be appreciated. Thanks.
Your answer
Follow this Question
Related Questions
2D Combo Attack 1 Answer
Platform color to kill player if player does not match 2 Answers
Play 2D animations via trigger parameters 1 Answer
Best way to handle 2d hitboxes for combat system 1 Answer
Spritesheet and 2D control 0 Answers