Randomizing the same animation transition on multiple instances of a prefab?

You can do it cheap and cheerful with a coroutine, something like this:

IEnumerator RandomlyChangeColors()
{
   const float MinTime = 4.0f;   // you adjust these obviously
   const float MaxTime = 8.0f;

   while(true)
   {
      yield return new WaitForSeconds( Random.Range( MinTime, MaxTime);
      gems.SetBool(("ChangeGemToRed"), false);
      gems.SetBool(("ChangeGemToBlue"), true);

      yield return new WaitForSeconds( Random.Range( MinTime, MaxTime);
      gems.SetBool(("ChangeGemToBlue"), false);
      gems.SetBool(("ChangeGemToRed"), true);
   }
}

In your Start function you would do:

StartCoroutine( RandomlyChangeColors());

Each script instance will pick its own random time (based on MinTime/MaxTime variables) and I think that’s what you want.

Have you looked into using a Trigger type animator parameter? That way you don’t have to change the old one to false:

Once the trigger is acted on by the animation, it gets automatically cleared.

Also, for future code postings, please use code tags:

1 Like