Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was wondering how I can store these anims inside an array, because, the start function will be fired one time. I would like to be able to call these Animators more then one time.

    public Animator anim0;
    public Animator anim25;
    public Animator anim50;
    public Animator anim75;
    public Animator anim100;

void Start () {

        anim0 = GameObject.FindGameObjectWithTag("Campfire0").GetComponent<Animator>();
        anim25 = GameObject.FindGameObjectWithTag("Campfire25").GetComponent<Animator>();
        anim50 = GameObject.FindGameObjectWithTag("Campfire50").GetComponent<Animator>();
        anim75 = GameObject.FindGameObjectWithTag("Campfire75").GetComponent<Animator>();
        anim100 = GameObject.FindGameObjectWithTag("Campfire100").GetComponent<Animator>();

    }
share|improve this question

closed as off-topic by Byte56 Mar 31 at 14:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 1 down vote accepted

what about:

Animator[] anims = new Animator[number_of_animators];

anims[0] = anim0;
...

if you mean to have an array editable inside the editor you can declare it as public and you will specify inside the editor its size and you will be able to drag and drop elements into it

just like this:

public Animator[] anims;

this is to be declared just as variable. No need to initialize inside Start()

then to check which animation is being played just declare

AnimationInfo[] animPlayed;

and when it is needed call

animPlayed = you_animator.GetCurrentAnimationClipState (0);

which returns the first animation clip being played (because you may have more concurrent animation)

share|improve this answer
    
I think I will give it a try. To make the animators public, just set public before the first line? And should I also make a foreach loop to create the lines like: anim0 = GameObject.FindGameObjectWithTag("Campfire0").GetComponent<Animator>(); or is it just anims[0] = anim0;, anims[25] = anim25;? –  Caspert Mar 31 at 12:16
    
see my edit. I added how you could have an array that can be modified in the editor –  Leggy7 Mar 31 at 12:25
    
Thank you for your fast response. Should I set it inside the Start function or above? And what should I set in number_of_animators? –  Caspert Mar 31 at 12:33
    
I edited again. Please consider editing your quesiton with your other question –  Leggy7 Mar 31 at 12:47
    
Sorry for all the questions, I am quit new to Unity. But does number_of_animators exactly mean? Should I insert a number instead of it or create a variable? @Leggy7 –  Caspert Mar 31 at 18:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.