I have a plane with a texture on it. The texture is looping through a bunch of materials assigned to it to give the idea of an animated sprite.
i.e
public void StartAnimation()
{
active = true;
renderer.enabled = true;
_animating = true;
_animationIndex = 0;
_firstShown = false;
}
void Update()
{
if (_animating)
Animate();
}
private void Animate()
{
// Loop through the materials to animate
_animationIndex = Convert.ToInt32(Time.time / changeInterval);
_animationIndex = _animationIndex % materials.Length;
renderer.sharedMaterial = materials[_animationIndex];
// If the final material is reached, stop animating and hide the gameobject
if (_animationIndex == materials.Length - 1)
{
_animating = false;
renderer.enabled = false;
active = false;
_animationIndex = 0;
}
}
The problem is I need to show this animating sprite as it goes through its animation loop, then hide it. Then if I show it again, it must start at the beginning of its loop, then disappear again. Whats happening is as I show it, it seems to randomly be starting at certain frames of the animation loop, and never starting from the beginning. I’ve enabled/disabled it yet it still seems random.
Thanks