How should I sync animations?

So I have a custom script that takes in an array of sprites, and animates them frame by frame. It does this by invoking my custom IncreaseFrame function every 0.1 seconds (and changes the sprite to the next in the array). I have multiple sprites using this, but I want all of their animations to line up because they are conveyor belts, and conveyor belts should be moving at the same pace at the same time. I am not sure how to achieve this. Here’s what I’ve played around with:


  • Creating a master script that calls IncreaseFrame remotely in every sprite animator script at the same time, but I can’t figure out how to call the function in every script, and prefabs (the conveyors) won’t allow me to manually assign a script that’s not a prefab (i.e. my game controller gameobject would have the master script).

  • Inheritance, where all of the derived scripts are the sprite animator, but since it’s class inheritance all of the base classes would still be separate objects, and would only start animating when created so they would still not be synced.

  • GameObject.SendMessage, which calls the function of all child objects, but I feel like it would get messy to put all of my prefabs under one parent object, although this might be the best option.

  • Somehow use Time.deltaTime to line them up, but I’m not sure how…


Any ideas would be very much appreciated! I have added a gif example of my game, all the conveyors animations should be lined up: 160163-y8musoy5wv.gif

I think I should be able to get a list or array of all gameobjects with the animator script and loop through it, activating them individually.

Edit: Yup! I figured it out.

// Switch frame
    public void SwitchFrame() {
        // Increase frame
        frame++;

        // Get every script that is of sprite animator
        SpriteAnimator[] spritesToAnimate = GameObject.FindObjectsOfType<SpriteAnimator>();
        for(int i = 0; i < spritesToAnimate.Length; i++) {
            spritesToAnimate_.frame = (frame % spritesToAnimate*.frameSPR.Length);*_

spritesToAnimate*.UpdateFrame();*
}

// Invoke in a certain amount of seconds, sort of used as speed
Invoke(“SwitchFrame”, frameDuration);
}
Hopefully this is useful to any other folk who aren’t using the Animator.