How to specify object in getcomponent?

0

How do I specify each object in getcomponent ? I did this script, but I do not know how to 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");
    }
}
    
asked by anonymous 21.07.2017 / 20:01

2 answers

1

Let's suppose you have 3 objects and you need to run 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 like you've omitted a parameter in the Play method call, take a look in the Unity documentation for more information.

    
21.07.2017 / 22:50
0

I think 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 think it's the easiest and lightest way. You can do it by code as well, but you will have to say in void Start what animators are, because in your code they are null, there are only 3 variables that can receive instances of type Animator, but that does not have anything yet, int or float, give name but not speak what 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>();
}
    
30.08.2017 / 03:42