how to reference a list that is inside a scriptable object class from a monobehaviour

I need to create a list of enemies but didn’t want to make it in the monobehaviour derived class witch is attached to the enemy because I just need one class(list) for the whole game.

So I created a scriptable object and created the list there. Now I need to add in this list the game object that was instantiated (or delete if it was destroied). Although I’m having a hard time to make this reference thing Works. Here is the example:

public class EnemyStateController : MonoBehaviour
{

public ListOfActiveEnemiesObj enemyListInstance;

private void Awake()
{
    
        ListOfActiveEnemiesObj enemyListInstance = ScriptableObject.CreateInstance(typeof(ListOfActiveEnemiesObj)) as ListOfActiveEnemiesObj;

}

private void Start()
{   
    enemyListInstance.addToList(this);

}

}

public class ListOfActiveEnemiesObj : ScriptableObject
{

public List<GameObject> enemyList;

public void addToList(EnemyStateController enemy)
{
    enemyList.Add(enemy.gameObject);
    Debug.Log(enemyList);
}

}

So basically I need to connect both classes so I can call the function addToList()

ScriptableObjects aren’t meant to be edited at runtime, and if you need to edit your data during runtime, then using a ScriptableObject is not a good solution to the problem.

There’s a few ways to do what you need to do. If you need a single, globally accessible data structure that doesn’t need references to assets or scene objects until after the game starts, a basic class (not a MonoBehaviour or ScriptableObject) with static fields works well:

public class EnemyManager
{
	public static List<GameObject> enemyList = new List<GameObject>();

	public static void addToList(EnemyStateController enemy)
	{
		enemyList.Add(enemy.gameObject);
		Debug.Log(enemyList):
	}
}

// then in your other script

public EnemyStateController : MonoBehaviour
{
	private void Start()
	{
		// you don't need a reference to use public static fields
		EnemyManager.addToList(this);
	}
}