Model Animation with an Array

I’m working on a project where I have a series of models that I’m trying to animate using an array. There are 12 models total. The idea is that when I set each model on/off in the game environment as I iterate through the array, it will give the illusion of animation. The reason why I’m doing this is because I have no real experience with actual animation techniques, and this series of models is what I was provided with. I figured that this might be the best that I could do, but is this idea even feasible to begin with? Any insight on this would be greatly appreciated. I also have the code that I’ve written for the script I’ve been working on as well. Hopefully the logic for the AnimateModel method is sound, but if not please let me know. I hope that I’ve made both my problem and code clear enough. If not I would be happy to try and offer further clarity. Thanks.

EDIT: As I stated in a comment below, unfortunately something is not working correctly. I’m not getting the desired result. When I run the game all of the models seem to appear together at the same time. Once I stop the game all of the models are then turned off as well. There’s no hint of the proper animation during runtime that I’m looking for. When I call the AnimateModel method in update it causes Unity to crash as well.

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

public class ModelUpdate : MonoBehaviour
{
	//Public and private objects/variables
	public GameObject[] models = new GameObject[12];
	public bool running = true;

	private bool firstPass = true;
	private int i = 0;

	//LoadArray iterates through the models array, instantiating GameObjects
	void LoadArray()
	{
		for (int i = 0; i < models.Length; i++)
		{
			models *= new GameObject();*
  •  }*
    
  • }*

  • // Use this for initialization*

  • // Turning all of the models off to start with*

  • void Start()*

  • {*

  •  for(int i = 0; i < models.Length; i++)*
    

{
_ models*.SetActive(false);_
_
}_
_
}*_

_ /
* The primary method for iterating through the array. If during the first pass i == 0, don’t bother with turning the previous model off.
* If i == 0 and it’s no longer the first pass, turn off model 11 and turn model 1 back on.
* If i == 11, turn off the model that preceded it, turn model 11 on, set firstPass to false as it’s no longer the first pass, and set i == 0 to restart the loop.
* Otherwise iterate through the array, turning off the previous model first and turning the current model on to give the appearance of animation.
* As long as the end of the array has not been reached, it is safe to increment i by one.
/_
_
void AnimateModel()
_
* {*
* while(running)*
{
* if (i == 0 && firstPass)*
{
_ models*.SetActive(true);
}
else if(i == 0 && !firstPass)
{
models[11].SetActive(false);
models.SetActive(true);
}
else if(i == 11)
{
models[i - 1].SetActive(false);
models.SetActive(true);
firstPass = false;
i = 0;
}
else
{
models[i - 1].SetActive(false);
models.SetActive(true);
}*_

* if(i != 11)*
{
* i++;*
* }*
}
}

* // Update is called once per frame*
* //Originally AnimateModel() was called here, but this resulted in the program crashing with every run.*
* void Update()*
* {*
* //AnimateModel();*
* }*
}

If you’re activating every object in a while statement, then yes, all of your models will seem to appear at the same time. A while statement loops hundreds of times a second (depending on the complexity of your code inside said while statement). You need to implement something like a coroutine if you want your animations to start at different time intervals (https://docs.unity3d.com/Manual/Coroutines.html). Your models are turning off because you’re initializing them in the code. Once the code stops, your models will disappear. The Update method gets called every time there is a new frame, so if you have complex code inside AnimateModel() then your program will crash because said method is repeatedly getting called. I’m not sure what you have in that method so I can’t say for sure if that’s the problem. Are you using an animation component on the GameObjects? I’m having trouble trying to understand what’s going on with AnimateModel() and why you’re calling it every frame.