- Home /
This question was
closed Mar 03, 2017 at 02:41 PM by
PresidentPorpoise for the following reason:
Other
Question by
PresidentPorpoise · Dec 14, 2016 at 02:29 PM ·
animationscripting problemscripting beginnerscript error
Playing an animation attached to another object via C# script?
Hello, I am attempting to play an animation when the player is close enough and clicks a certain object.
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorboardOpen : MonoBehaviour
{
private bool open;
private float distance;
public GameObject targetObject;
void Awake()
{
distance = 6;
open = false;
}
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 playerPosition = GameObject.Find("FPSController").transform.position;
if (Vector3.Distance(playerPosition, transform.position) <= distance)
{
if (open == true)
{
GetComponent<Animation>().Play("Down");
open = !open;
}
else if (open == false)
{
targetObject.GetComponent<Animation>().Play("Up");
open = !open;
}
}
}
}
}
So far, everything works except that I get the error "The animation state Up could not be played because it could not be found!"
How do I fix this? The animation is set to Legacy and so is the object's import settings. The animation does play when play automatically is checked, so the animation itself does work. The object that the animation is attached to is a parent of the one the script is attached to, if that is helpful. Thanks in advance.
Comment