Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Reference scriptable object in editor script

Discussion in 'Scripting' started by will_brett, Jul 2, 2014.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hi everyone.

    I am trying to make and editor window that loads and saves stuff to/from a scriptable object.

    The scriptable object contains a list.

    How do I acces a scriptableobject from an editor extension script. Usually you would drag it to the slot on the script but as the script is not attached to a gameobject this is not possible.

    Sorry if this doesnt make sense.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    What do you mean by "access a ScriptableObject"? Create one? Select the file?
     
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    So what I mean is usually you would do something like

    public ScriptableObject _scriptableObject;

    Attach the script to a game object and drag the scriptable object into the object field. However as I am creating an editor window the script is not being put on a game object so how do I add a new list item to my scriptable object?

    hope that makes more sense.

    Cheers
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    So far so good...

    Tried selecting the .asset file? It should show up all the value of your ScriptableObject in the Inspector.
     
  5. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Ok im still very confused.

    Im not trying:

    ScriptableObject asset = (ScriptableObject)Resources.Load("newScriptableObject");

    Then Im trying this to add to the list in that ScriptableObject:

    asset.list.Add( new List(_newName, _newColor));

    However I am getting "is inaccessible due to it protection level." The list is a [Serilizedfield]
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    This means your field is private or protected and cannot be accessed from another class.
     
  7. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    I've made it public but its posting a load of other errors. I think this are separate to this question so I will try and sort those and come back. Thanks for your help though
     
  8. majesty_c

    majesty_c

    Joined:
    Sep 5, 2020
    Posts:
    1
  9. Pawel_Legowski

    Pawel_Legowski

    Joined:
    Oct 28, 2015
    Posts:
    4
    @majesty_c

    I suppose you need to use OnOpenAssetAttribute like that:

    class MyEditorWindow : EditorWindow
    {
    MyScriptableObject _editedAsset;

    //This will happen on double clicking asset
    [OnOpenAssetAttribute]
    public static bool OpenData(int instanceId, int line)
    {
    //We check if the double clicked asset is of desired type
    var editedAsset = EditorUtility.InstanceIDToObject(instanceId) as MyScriptableObject;
    //If its not we return false
    if (editedAsset == null)
    {
    return false;​
    }

    //Create window, pass double clicked asset with some function and return true
    CreateWindow<MyEditorWindow>("Name", typeof(SceneView)).Init(editedAsset);
    return true;​
    }

    void Init(MyScriptableObject obj)
    {
    _editedAsset = obj; ​
    }​
    }
     
  10. nareshbishtasus

    nareshbishtasus

    Joined:
    Jun 11, 2018
    Posts:
    35
    Code (CSharp):
    1. string soPath = "Assets/Funnel/UID/UniqueBuildID.asset";
    2.         var so = AssetDatabase.LoadAssetAtPath<SO_UID>(soPath);
    3.  
    4.         Guid g = Guid.NewGuid();
    5.         so.uniqueBuildID = g.ToString();