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

best way to create a (static) reference to a scriptableobject for editor-time?

Discussion in 'Scripting' started by AbandonedCrypt, Dec 23, 2020.

  1. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    69
    Hey, right now I'm using AssetDatabase.LoadAssetAtPath(), however whenever I want to load a SO-asset outside of an OnEnable() method, I get an error.

    Ideally I need a static reference to my SO's to create proper helper classes, because the way it is right now, I have to create these references very hacky through instantiating a helper class object in OnEnable() of each editor that uses it, which is a proper nuisance, as I would like to have the helper classes be static.
     
  2. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    Is it possible for you to just have it assign in a public variable via the inspector?
     
  3. AbandonedCrypt

    AbandonedCrypt

    Joined:
    Apr 13, 2019
    Posts:
    69
    I guess that would be another hacky way, however I am writing my editors with portability in mind, as I plan to use them in multiple projects and would therefore like them to be drag-and-drop-able without having to create scene objcts, since they are solely database-oriented.
     
  4. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    400
    yeah, I had this issue and couldn't find a solution. I think you can't do it on enable, but I may be wrong. If you don't use them on enable (you probably shouldn't anyway) you could try to use them a bit like this to only loaded if it's null, and leave it from enable:
    Code (CSharp):
    1. public Databases_Ref Db { get { if (_db == null) { _db = (Resources.Load("Databases") as GameObject).GetComponent<Databases_Ref>(); } return _db; } }