Is there any way to receive callbacks from the Unity Engine without creating a MonoBehaviour?

Is there any way to write a class (not inheriting from MonoBehaviour) that somehow can itself subscribe to an Unpdate or other Events of the Unity Engine?

I’d like to write a Utility that does some custom GL drawing - but the only way so far I see is by creating a GameObject that will have an attached monoBehaviour to receive OnRenderObject events. Is there no way around this?

No there is no way around this, and why would there be :slight_smile:

You can make a game object and attach a script to call your OnRenderObject in your custom class though. And you can do that at run time rather than in the Editor and if you like you can hide it from the Editor view too:

#Support.cs

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class Support : MonoBehaviour
{
	public static Support instance;

	void Awake()
	{
		instance = this;
	}

	public OtherClass owner;

	void Update()
	{
		if(owner != null)
			owner.Update();
	}
}

#Example.cs

using UnityEngine;
using System.Collections;

public class TestAddComponent : MonoBehaviour {

public OtherClass someClass;

}

[System.Serializable]
public class OtherClass
{
	public string something;


	public virtual void Update()
	{
		Debug.Log("Updating");
	}

	public OtherClass()
	{
		if(!Support.instance)
		{
			var go = new GameObject();
			go.hideFlags = HideFlags.HideAndDontSave;
			var support = go.AddComponent<Support>();
			support.owner = this;
		}
		else
		{
			Support.instance.owner = this;
		}
	}

}

You can easily roll this yourself if you already have a manager/controller style game object (or even something constant like the camera etc). Just add a callback mechanism in it’s main script that allows non-MonoBehavior scripts to register to be called during Update(). I’m not sure about JS but in C# you can create an Interface that the non-MonoBehavior scripts can inherit from, and you can use the same Update() call signature to keep things clear. I use this technique to add Listeners to some key game events I want to track, so I can use thin utility scripts to respond rather than full fat MonoBehavior classes.

Hello @temptest123 I think that the best answer is being overlooked altogether, ```Is there any way to write a class (not inheriting from MonoBehaviour) that somehow can itself subscribe to an Update or other Events of the Unity Engine?

I’d like to write a Utility that does some custom GL drawing - but the only way so far I see is by creating a Game Object that will have an attached monoBehaviour to receive OnRenderObject events. Is there no way around this?

Add commentfrom you statement and question here has no one suggested an abstract class to you or have you not ran across it in the api becausethat somehow can itself subscribe to an Unpdate or other Events of the Unity Engine?this right here is pritty much aht it boes as an exsample here is an enemy ai class i wrote for a platformer for a client.using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// doing it this way sets an abstract class so all of the enemy AI will feed off this one script as a base for there movement animations and such with there special actions for that enemy set in there own.
public abstract class TS_Guardian_Enemy : MonoBehaviour
{
// serializefiels on the variables so the base stats can be set in the inspector on each enemy.

// this one is for the enemys healt.
[SerializeField]
protected int _health;
// the speed at which the enemy moves from point to point.
[SerializeField]
protected int _speed;
// the amount of in game currency the enemy can and will drop.
[SerializeField]
protected int _emeraldLeaf;
// the transition points in which the enemy will patrol.
[SerializeField]
protected Transform pointA, pointB;

// a base line attack method that all enemies will be able to preform without having to write it in there script.
public virtual void _attack()
{
    Debug.Log("BaseAttack Called!");
}

// the base absrtact update method for the enemy AI in the set.
public abstract void Update();

}here is the main base enemy AI abstract class script and then it controles this named enemy subclass some what like a helper script.using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using TS_Guardian;

public class TS_Guardian_Enemy_AI_plagueSquirrel : TS_Guardian_Enemy
{

private bool _switching = false;

// in the void start set the ability to call the base attack function set in the main enemy script.
private void Start()
{
    _attack();
}

// inorder to function correctly the void updat has to be an override to over ride the base enemy script actions to add custom movement update and attacks spicific to the enemy its on.
public override void Update()
{
    if (transform.position == pointA.position)
    {
        Debug.Log("Point A");
        _switching = false;
       
    }
    else if (transform.position == pointB.position)
    {
        Debug.Log("Point B");
        _switching = true;
    }

    if (_switching == false)
    {
        // move right
        transform.position = Vector3.MoveTowards(transform.position, pointB.position, _speed * Time.deltaTime);
    }
    else if (_switching == true)
    {
        // move left
        transform.position = Vector3.MoveTowards(transform.position, pointA.position, _speed * Time.deltaTime);
    }
}

public override void _attack()
{
    
    Debug.Log("Plague Squirrel put the smack down on ya!");
}

}```

Yes: Unity - Scripting API: EditorApplication.update