Search Unity

Create ScriptableObject in editor for use during runtime

Discussion in 'Scripting' started by gyrosp, Sep 4, 2015.

  1. gyrosp

    gyrosp

    Joined:
    Jul 29, 2015
    Posts:
    10
    Hi.

    I hope I can put my problems into the right words :).

    I'm writing an game where the user can create recipes. If the recipe matches a predefined recipecombo something happens.

    Right now I'm writing the editor script for creating the predefined recipecombos. As any recipe can consist of different ingredients and any ingredient can be added multiple times I have created a helper class NeededIngredient which inherits ScriptableObject. It just holds a reference to an ingredient and the min and max value of the ingredient.

    When the user adds a new NeededIngredient to the combo in the editor I create a new instance of this helper-class and store it in a list like this:

    Code (CSharp):
    1.     private void mnuAddNeededIngredientClickHandler(object param)
    2.     {
    3.         if (param == null)
    4.             return;
    5.  
    6.         NeededIngredient neededIngredient = ScriptableObject.CreateInstance<NeededIngredient>();
    7.         neededIngredient.ingredient   = (IngredientBase) param;
    8.         neededIngredient.minNeeded    = 1;
    9.         neededIngredient.needMaxValue = false;
    10.  
    11.         _dataListNeededIngredients.Add(neededIngredient);
    12.  
    13.         EditorUtility.SetDirty(target);
    14.     }
    This works fine within the editor during design-time but in runtime the reference to the ScriptableObject is lost and the game crashes.

    scriptableObject.jpg

    After a lot of googling I found out that CreateInstance() should only be called during runtime or that I have to save the ScriptableObject to an asset.

    But I can't believe that this is the solution. This is a simple helper class and I don't want to create any assets or something similar.

    Any help in solving this issue is really appreciated.
     
  2. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    We do something similar with our Items. But yes, you need to save them as assets. Really, it's not cumbersome. And it should void the need for a custom editor.
     
  3. gyrosp

    gyrosp

    Joined:
    Jul 29, 2015
    Posts:
    10
    Sometimes Unity is really great and easy, sometimes Unity is really, really **cking me up for things that should be very very simple but are not in Unity :(.

    This helperclass will be used a lot. There might be hundret of instances. Do I need to create an asset for every instance used at runtime or do I need to create only one?

    If I do need more than one is there any other solutions to solve my problem as it will surely not be managle to have an asset for every instance.
     
    Last edited: Sep 7, 2015
  4. gyrosp

    gyrosp

    Joined:
    Jul 29, 2015
    Posts:
    10
    I now replaced ScriptableObject with a plain serializable C#-class and it is working fine.

    Why is ScriptableObject such a pain?! Why using ScriptableObject when a plain C#-Class is serializing well?
     
  5. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    only 4 years since this thread was alive? Better stick an irrelevant subjective answer to it!

    ScriptableObjects shine when you need global functionality not constrained by the currently loaded scene or when you're going for a modular approach.

    Imagine your game designer to be able to tweak and test functionality of your game just by drag'n'dropping scriptable objects into appropriate slots instead of coming back to you with "Can you please edit this little bit so that I can see what it does?" 1000 times a day.