Home > Enterprise >  Creating an array/list of scripts for a Scriptable Object
Creating an array/list of scripts for a Scriptable Object

Time:07-22

So, I have an ItemObject which is a ScriptedObject

In an ItemObject I want to create an array/list that stores the scripts in it

The scripts that will be stored in this array/list will be used to extend the functionality of the GameObject (Imagine as unique skills for player, not attributes)

To do this, I'm going to add the script as a component with Player.AddComponent(SCRIPT.GetType())

So, it is assumed that after I create an item asset in the project directory, I can select the number of script skills and add them through the inspector

Also, as a sad alternative, I was considering using GameObject, which would store scripts as components. And that, to me, is terribly unhandy, given that there could be hundreds of the scripts. And unfortunately, I'm still not experienced enough, so it's the best I've come up with so far.

CodePudding user response:

You could do something like

using UnityEngine;

public class ItemObject : ScriptableObject{
    [SerializeField] List<MonoBehaviour> scripts;
}

Although, if I'm understanding the objective correctly, maybe it would be a good idea to have a look at the concept of prefabs instead.

CodePudding user response:

I solved my problem in the following way: By adding the script as an Object and displaying its type in the console, I found out that it is a MonoScript

So just by adding using UnityEditor I was able to create a list consisting of MonoScript

using UnityEditor;
class Script : MonoBehaviour
{
    List<MonoScript> scripts
}

And then, using [GameObject].AddComponent([MonoScript].GetClass()) I was able to add this script as a component to the GameObject

class AddScriptAsComponent : MonoBehaviour
{
    GameObject object;
    MonoScript script;
    void Add()
    {
        object.AddComponent(script.GetClass())
    }
}
  • Related