- Home /
Reusing animation but they sync
Hello there.
I am trying to make a night sky with some of stars (2D game, btw). So I created a star prefab with an animator control on it and a script that randomly positions my star and tells it to wait a random number of seconds to start the shining animation (code below); when I put multiple star prefabs they start shining with random delay, which is what I expected, but suddenly they all sync... Their animations sync and it doesn't seem random at al. Any idea?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Star : MonoBehaviour
{
Vector2 rndPos;
float rndPx;
float rndPy;
RectTransform myRT;
void Start ( )
{
rndPx = Random.Range ( 0.05f, 0.95f );
rndPy = Random.Range ( 0.5f, 0.95f );
myRT = GetComponent<RectTransform>( );
rndPos = new Vector2 ( rndPx, rndPy );
myRT.anchorMin = myRT.anchorMax = rndPos;
StartCoroutine ( initAnim ( ) );
}
IEnumerator initAnim ( )
{
yield return new WaitForSeconds ( Random.Range ( 0f, 3f ) );
GetComponent<Animator>( ).SetBool( "animated", true );
}
}
Thanks in advance!
Answer by jrobertomar · Sep 21, 2015 at 06:40 PM
Ok, I think I came with a work-around...
Instead of this:
GetComponent<Animator>( ).SetBool( "animated", true );
I used this:
GetComponent<Animator>( ).Play ("Star Animated");
But I still wonder why my first approach didn't work. It seems that the animations on each star "started" at the same time, even if I set the boolean to true at different times