Convert Object to Scriptable-Object

How can i convert Objects to Scriptable objects or is there a better way to load Scriptable Objects out of the Resource folder?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ItemDatabase : MonoBehaviour
{
    public Object[] Items;
    public Item items;

    void Start()
    {
        Items = Resources.LoadAll("Items", typeof(Item));

        foreach (var t in Items)
        {
            Debug.Log(t.name);

        }
    }
}

ALWAYS use the templated forms: Resources.Load() and Resources.LoadAll

For instance

private MyScriptableObjectType[] Items;

and then in Start():

Items = Resources.LoadAll<MyScriptableObjectType>( "Items/");

The reason it is critical to use the templated forms is because if you have two assets named the same (for instance Foo.png and Foo.fbx), it is a 50/50 proposition which one Unity will give you unless you use the form of the call.

1 Like