Hello, I am trying to run an animation when the player clicks on an object with the script attached. This is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ContainerOpen : MonoBehaviour
{
void OnMouseOver()
{
if (Input.GetMouseButton (0))
{
animation.Play ("ContainerOpen");
}
}
}
I get an error saying that Unityengine.component does not contain a definition for “Play”. How do I fix this?
EDIT:
I have fixed the error, but I still cannot get the animation to play. Can anyone help me? Both Debug.Logs are successful in my code, so I have no idea why the animation is not playing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ContainerOpen : MonoBehaviour
{
void OnMouseOver()
{
Debug.Log ("screw you, misster magoo");
if (Input.GetMouseButton (0))
{
Debug.Log ("you clicked this piece OperatingSystemFamily shit, now good luck playing an animation");
GetComponent<Animation>().Play ();
}
}
}
Accessing other components on a game object through properties such as animation or rigidbody are deprecated, and I wouldn’t be surprised if they have undergone breaking changes that makes old code no longer work.
Instead, try GetComponent<Animation>().Play("ContainerOpen"); Going forward, GetComponent<TComponent>() is the recommended way to get another component on a game object, whether it’s one of Unity’s built-in component types, or one of your own.
EDIT: I’m slow, you already figured out that problem.
As for it not actually playing the animation, your second example left out the name of the animation, and so only the default animation will play. Is the default animation the one you want?
How do I set which animation I want to be default? I only have one animation attached to the object that the script is also attached to.
Edit: I know the right animation is playing, since the same happens when I check “Play automatically” on the animation. It must be an animation issue and I have asked on the Animation forums, but I kind of doubt I will get help there since my previous help post still lacks any replies at the time of writing this.