Hello, I am trying to get an animation to play when I click an object with the attached animation. I used OnMouseOver to check if the mouse is hovering over the object, then I used Input to get the left mouse button. After the input is checked, an animation is coded to play. Problem is, it will not play.
I get no errors with this script, but nothing happens. My Debug.Log’s return the strings to the console, so I know my crap is working. Please, can someone help me? I gotta go to sleep.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ContainerOpen : MonoBehaviour
{
void OnMouseOver()
{
Debug.Log ("screw you");
if (Input.GetMouseButton (0))
{
Debug.Log ("you clicked this piece of shit, now good luck playing an animation");
gameObject.GetComponent<Animation>().Play("TopLeftDeskDrawer|OpenClose");
}
}
}
Hey Porpoise,
I explained everything in the code. If you have questions just ask me:
using UnityEngine;
using System.Collections;
public class animTestIt : MonoBehaviour
{
//You need to instantiate the "animator" to use it.
public Animator anim;
// Use this for initialization
void Start()
{
//Here you instantiate the "animator" to use it later on.
anim = gameObject.GetComponent<Animator>();
//You disable the "animator" so the animation won't play immediately
anim.enabled = false;
}
void OnMouseOver()
{
if (Input.GetMouseButton(0))
{
//Activate the Animator to play the Animation.
anim.enabled = true;
anim.Play("TopLeftDeskDrawer|OpenClose", 0);
//Start the timer.
StartCoroutine(WaitTillAnimOver());
}
}
//This is the timer. After the time, you put into the braces of "WaitForSeconds", is over, the command after this line will run.
IEnumerator WaitTillAnimOver()
{
yield return new WaitForSeconds(1f);
//Disable the "animator" to prevent the animation from playing everytime.
anim.enabled = false;
}
}