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

Need help with ScriptableObject.CreateInstance

Discussion in 'Scripting' started by Pysassin, May 9, 2014.

  1. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I have ScriptableObject assets that store information needed for my items/weapons. The mono script links to these ScriptableObject then uses/modifies those stats as needed. At the moment though they are linked to the parent asset and changing stats on the parent, I assume I need to create an instance of the parent asset, but not sure HOW I can use this script to create an instance of the right script procedurally. I assume on Fx Start I would make the myClass variable (the stats I will be able to change) equal to ScriptableObject.CreateInstance(). What I'm unclear on is how I would pass the argument the give me the desired effect. I need something that can work procedurally on many types of scriptable assets so that each item doesnt need its own script made to assign this instance.

    I know that sometimes my posts are a bit unclear so if something doesn't make sense say something and I'll try to explain a different way.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    How come you're using ScriptableObjects and not MonoBehaviours? My understanding is that shared data is the intended use case for ScriptableObject, so there's the potential you're making life harder than it has to be, though I don't know the rest of your use case.

    Anyway, I think what you want is Instantiate:
     
  3. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I use ScriptableObjects because my inventory items won't have gameObjects in the scene while inside the inventory. Many junk items etc won't ever have GameObject representation so having scriptableObjects is better for my project.
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Have you considered using native classes?
     
  5. hadiai

    hadiai

    Joined:
    May 10, 2014
    Posts:
    9
    Sounds to me you could live with a regular C# (System.Object) class (markedup with [System.Serializable]) instead of a UnityEngine.ScriptableObject. Use ScriptableObjects when you have like a settings file, etc.