- Home /
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()
Answer by Commoble · Mar 19, 2017 at 05:27 PM
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);
}
}
Thank you for the answer.
Let me get it right then. Scriptable object is used when we don't need to change anything inside of the class and so they are unchangeable, therefore they are not instantiated because, if I get it right, they are a static global piece of data, right? So a good use of them is if many objects(like an enemy) share the same mechanic, so their monobehaviour can call the scriptable object, pass the reference and then do what they need to do and return values for each of them. Also, if I need a script that doesn't need to be instantiated and can be used by other objects but need to be able to change some data then a basic class can be used for it?
Your answer
![](https://koobas.hobune.stream/wayback/20220612113456im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Losing reference to monobehaviour after updating a dll 0 Answers
How do you save a reference to a prefab's script in a ScriptableObject? 0 Answers
Unity Editor Animating object reference. 1 Answer
Pass ScriptableObject field into a function by reference 2 Answers
Having a reference to only one variable. 2 Answers