How to specify object in getcomponent?

Asked

Viewed 335 times

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 (with null) this will generate an error when trying to invoke GetComponent. If what you want to do is allow external reference (via the Inspector editor), the answer you have is correct: just add the public in front of the statement. Otherwise, explain your difficulty better, ok?

  • 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 method SetTrigger.

  • 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.

2 answers

1

Let’s assume that you have 3 objects and that you need to rotate the animations (the situation was not clear, if I got it wrong please correct me).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class anim2: MonoBehaviour 
{
    public Animator optionsubir; //Ao dizer que Animator é public, no
    public Animator subirplay2; //editor do Unity você pode arrastar o
    public Animator extralado; //gameObject que contem o Animator 
                              //desejado 
    public void ChamarAnimacao() 
    {
        optionsubir.GetComponent<Animation>().Play ("optionsubir");
        subirplay2.GetComponent<Animation>().Play ("optionsubir");
        extralado.GetComponent<Animation>().Play ("optionsubir");
    }
} 

It also seems that you have omitted a parameter in the Play method call, take a look in the Unity documentation for more information.

  • type, I’m wanting to pull 3 animations that are on different objects in just one

  • I saw on a page it said that to be able to pull the others had to specify each gameobject, this I am not able to do.

  • You can do so: declare at the beginning of code 3 public Gameobjects. Keep your image the way it is. In function Start() do so: optionsubir = nomeDoGameObject.GetComponent<Animator>();. Daí optionsubir.animation.Play("nome da animação") when playing in the animation.

0

I believe Raphael’s answer is right, you just need to make the variables public, so in the inspector, you can just drag the corresponding animations. I believe it’s the easiest and easiest way. You can do it by code too, but you’ll have to tell in void Start which animators are, because in your code they are null, are only 3 variables that can receive Animator type instances, but that has nothing yet, it’s like creating an int or float, give name, but do not say what the value, it will be null. What you would have to do to say what the "value" of your Animators is something like:

Animator optionSubir;

void Start
{
    optionSubir = GameObject.Find("nome do objeto com a animação").getComponent<Animator>();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.