Using Singleton on a class used on AnimationEvent, possible?

Hello guys,

I’m using one class that manages all animations events of my game.

But I would like it to have only one instance (Singleton).

The problem is that I need to attach it to the objects where I need an interaction during the animation, and its here on my problem comes up. When I set the Singleton pattern, it removes the other GameObjects that also use that same class.

Is there any solution for this? Or am I thinking the hard way?

public class MonoBehaviourSingletonPersistent<T> : MonoBehaviour
    where T : Component
{
    public static T Instance { get; private set; }
   
    public virtual void Awake ()
    {
        if (Instance == null) {
            Instance = this as T;
            DontDestroyOnLoad (this);
        } else {
            Destroy (gameObject);
        }
    }
}

Any help is welcome!

Cheers.

You’re definitely thinking in a confused way, or so it seems to me. You’re saying that you both want only one instance of this class, and you want many instances of it (one for each object that needs interaction during animation).

So, obviously you can’t have it both ways, and the first step is to decide which it is that you really want.

Perhaps you need two different classes — one that goes on each of the animating objects, and another singleton that lives to manage things at a more global level.

1 Like