0
How do I specify each object in the getcomponent
?
I made this script, but I can’t specify for each animator
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class anim2: MonoBehaviour
{
Animator optionsubir;
Animator subirplay2;
Animator extralado;
public void ChamarAnimacao()
{
optionsubir.GetComponent<Animation>().Play ("optionsubir");
subirplay2.GetComponent<Animation>().Play ("optionsubir");
extralado.GetComponent<Animation>().Play ("optionsubir");
}
}
Colleague, your question is not clear. What do you mean by "specify"? Your code is wrong because the declared attributes (
optionsubir
, for example) were not initialized, and how they are null (withnull
) this will generate an error when trying to invokeGetComponent
. If what you want to do is allow external reference (via the Inspector editor), the answer you have is correct: just add thepublic
in front of the statement. Otherwise, explain your difficulty better, ok?– Luiz Vieira
And just for the record, to separate the roles (programmer vs animator, for example) it is more common for the animator to build all the animation (which can include even more than an animation clip! ) and provide you with a "trigger" (a Trigger) that you invoke directly on
Animator
with the methodSetTrigger
.– Luiz Vieira
Invoke
Play
directly in an animation works, but it is not a good practice because you lose the flexibility that this feature animation state machines gives you.– Luiz Vieira