Adding custom class to update loop

Is there a way to put a custom class in the Unity’s update loop?

Something like this:

class MyClass
{

public MyClass()
{ Unity.OnUpdate += this.update; }

public void update(){}

}

If your class inherirt MonoBehaviour, the Update function is automatically called by the engine.

If you don’t want to inherit Monobehaviour, then no, there is no built-in way to do this.

In order for your class to have the Update message sent to it, it needs to be a MonoBehavior script and attached to a GameObject.

public class MyClass : MonoBehaviour
{
    void Start()
    {
        
    }

    void Update()
    {
        
    }
}

Alternatively you can create an instance of your class inside of a MonoBehaviour script and then call your class’s update from within that MonoBehaviour’s Update function.

public class MyBehaviour : MonoBehaviour
{
    MyClass myClass;

    void Start()
    {
        myClass = new MyClass();
    }

    void Update()
    {
        myClass.update();
    }
}

The MonoBehaviour scripts will need to be attached to an object in your scene though.