- Home /
Animation play in unity 5??? c#
Hello. I updated to unity 5 and everything changed here how do i play animation? i been using animation.play(); but now it does not work it give a error. Then i tried this:
void OnMouseDown (){
GameObject.GetComponent ().Play ();
}
ERROR: error CS0120: An object reference is required to access non-static member `UnityEngine.GameObject.GetComponent(System.Type)'
/\
|
|
It give me a error anyway.
I want that on onmousedown on the model it would play the animation when the animation ends it would stop. How do i do that?
Answer by Cherno · May 04, 2015 at 12:00 PM
Change
GameObject.GetComponent ().Play ();
to
GetComponent<Animation>().Play ();
or
gameObject.GetComponent<Animation>().Play ();
If you write GameObject with a capital "C", you refer to the Type "GameObject", but you want to refer to a specific instance of it (as the error messagetells you). Hence, write gameObject, with a "g". However, you don't need to write it in this case since writing GetComponent without anything before it automatically lets teh program know you want mean the GameObject this script is attached to.
$$anonymous$$y pleasure. However, This particular error is not related to the new Unity version. It might be possible that Unity converted the code in your script automatically to conform with Unity5's new syntax rules (no more "animation" ins$$anonymous$$d of "GetComponent(), for example), and out came the line of code you posted.
Answer by iterreno · Oct 03, 2016 at 06:01 AM
I my case used for example: private GameObject maleCharacter;
maleCharacter = GameObject.Find("MainMaleCharacter");
Animator animationObj = maleCharacter.GetComponentInChildren<Animator>();
animationObj.SetTime (1);
animationObj.Play("Back_Stab_Grab_Down_Finish_01_001");
Regards
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
UI Text Alpha not fading out in animation 0 Answers
Animation disturbing circular rotation 1 Answer
Animation at the end of the level 1 Answer