- Home /
Activate an animation by clicking on a separate Gameobject
I am trying to play my elevator animation that moves the elevator(gameobject) when I click on the button(gameobject) on the other side of the map. I've used Raycasting to click on the button but I don't know how to get the elevator to begin it's animation. I have two scripts, one on the elevator and one on the button. I not sure what I am missing though that makes the script recognize the animation. My scripts just allow me to click the elevator gameobject and then it animates. If I click the button, nothing happens. I don't want this effect. What am I doing wrong?
Im using Cs and I have this error message: MissingComponentException: There is no 'Animation' attached to the "First Person Controller" game object, but a script is trying to access it. You probably need to add a Animation to the game object "First Person Controller". Or your script needs to check if the component is attached before using it.
using UnityEngine;
using System.Collections;
public class ElevatorButton : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100))
{
hit.collider.animation.Play("Elevator");
}
}
}
}
Your answer
Follow this Question
Related Questions
Activating One Part of An Animation 2 Answers
Prevent animation from starting the first time 0 Answers
Animation works only once on button click 4 Answers