17

So apparently i suck at listening at my university, because i can't figure this out, not even with google... How do you create a scriptable object in the editor? I have the project open, it looks like this:

enter image description here

Click the Create button as if you wanted to create a folder or C# script or anything.

enter image description here

Select the ScriptableObject from the popup menu.

enter image description here

Get this panel and finalize the object after selecting the script for it.

The problem is: i don't have the ScriptableObject button. I have a script that is a scriptable object (to make sure i even copied the one from the project of the university). I restarted Unity, i checked if there were any packages installed (there werent) and i googled quite a bit. But i just can't seem to get this working...

Is there anything i have to install or add first? Thanks in advance!

2
  • Did you check this tutorial ? unity3d.com/fr/learn/tutorials/modules/beginner/… Commented May 28, 2018 at 10:53
  • ...I was watching it in the background (as its an hour long) and right as i posted this the explanation came. It works different than the uni project but its works ( using [CreateAssetMenu()] above the class). the funny thing is, the uni project doesnt do tha and it works for them... Commented May 28, 2018 at 10:59

2 Answers 2

39

You need another script to add the button which will create an instance from that scriptable object. something like that

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeScriptableObject {
    [MenuItem("Assets/Create/My Scriptable Object")]
    public static void CreateMyAsset()
    {
        MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();

        AssetDatabase.CreateAsset(asset, "Assets/NewScripableObject.asset");
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;
    }
}

You can check this Introduction to Scriptable Objects tutorial on unity website.

Sign up to request clarification or add additional context in comments.

2 Comments

this can be made quicker with adding [CreateAssetMenu()] above the class (which atomates the above code). Thanks so much! this was driving me crazy... the funny thing is, the university project i have open neither has the CreateAssetMenu nor does it have the code above... i'm just glad it's working now tho!
@Yiasmat That's not quite the same thing [CreateAssetMenu()] gives you no opportunity to run your own code during the creation, like assigning a unique id to the SO for example.
9

I can't comment so just place it like answer: Don't forget to use UnityEditor.AssetDatabase.GenerateUniqueAssetPath coz simple AssetDatabase.CreateAsset can erase your data:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeScriptableObject
{
    [MenuItem("Assets/Create/My Scriptable Object")]
    public static void CreateMyAsset()
    {
        MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();

        string name = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/NewScripableObject.asset");
        AssetDatabase.CreateAsset(asset, name);
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;
    }
}

1 Comment

You helped me figure out how to focus and select the new object too! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.