Adding Scriptable Object in a list from a folder

Hi there,

I am new to coding in C# and I stumble a bit on something. I created several Scriptable Objects from a CSV file, and I now want to populate a list with them automatically.
Here is my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.IO;
using Unity.VisualScripting.Generated.PropertyProviders;

[System.Serializable]
 public class Create_Target_List : MonoBehaviour
 {
    public List<ScriptableObject> TargetList = new List<ScriptableObject>();
    
    [MenuItem("Generator/Generate Target List")]
    public static void Generate_Target_List()
    {
        object[] TargetList = Resources.LoadAll("Cibles");
  
        foreach(object o in TargetList)
        {
            TargetList.Add();
        }

    }
    
}

I don’t know if the foreach is justified. But if it is, what should I put into the brackets after “TargetList.Add” ?

Thank you !

Quite a bit of the code isn’t right here. I’m assuming you want to just load all these scriptable objects into the monobehaviour’s list. To do that, you need a reference to it.

With MenuItem this can be done by adding a MenuCommand parameter, and using the CONTEXT syntax of Menu item to make it a right-click item specific to this monobehaviour.

Would look something like this:

public class Create_Target_List : MonoBehaviour
{
	public List<ScriptableObject> TargetList = new List<ScriptableObject>();
	
#if UNITY_EDITOR
    [UnityEditor.MenuItem("CONTEXT/Create_Target_List/Generate Target List")]
    public static void Generate_Target_List(UnityEditor.MenuCommand command)
    {
		var target = (Create_Target_List)command.context;
		
		ScriptableObject[] targets = Resources.LoadAll<ScriptableObject>("Cibles");
		target.TargetList.AddRange(targets);
    }
#endif
}

Hi ! Thanks for your answer ! It works well !

It showed me errors at first but for anyone interested, I just change “Context” to “context” line 9 and put the [ ] in front of “ScriptableObject” and not “targets” line 11.

Good call outs. That’s what happened when I write these in notepad++. I’ll edit the code.