Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ScriptableObject with multiple scripts

Discussion in 'Getting Started' started by Green11001, Jul 13, 2022.

  1. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    I am trying to make skills with scriptableobjects. The skills work by linking together unityevents with seperate classes for each effect. (Ex. skilldamage, skillapplyeffect, and a main skill class) However, I can't seem to attach more than one script to a scriptableobject
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I'm not entirely sure what you mean by "attach more than one script to a scriptableobject", but generally you'd want to declare an array/List variable of your script type to assign multiple references.

    If that's what you're already trying to do, then can you elaborate more on the issue?
     
  3. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Uh, its odd to explain this without giving an example but let me try
    Skills would be formatted like this

    Skill (UnityEvent -> SkillDamage, -> SkillEffectA, -> SkillDelay)
    SkillDamage
    SkillEffectA
    SkillDelay (UnityEvent -> SkillEffectB)
    SkillEffectB

    Where all of the above are scripts on a single gameobject
    I can change it from unityevents to inheritance and just call the method, currently unityevents are really only used for convenience

    The problem is that I don't know how to create all these scripts on a single scriptableobject
    I could probably do
    ScriptableObject 1
    SkillDamage

    ScriptableObject 2
    SkillEffectA

    But this would generate a ton of scriptableobjects and I don't want that.
     
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    All you need in your scriptable object is a list of parameters that you will use to apply the different effects. Since the scriptable object is a script itself it can invoke any class you have declared elsewhere, or make use of UnityEvents.
     
  5. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    I have considered that, but I don't want to create a lot of clutter through creating all the components as separate objects/files.
     
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,971
    But that's precisely how you are supposed to be using SOs. Trying to work against that is just going to make your life more difficult than it should be. That said if you must progress down this rabbit hole just create a List<> on the SO of a type appropriate to the base effect, add them to that, and then write method in the SO to operate on them.
     
  7. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Creating 3-4 seperate SOs for every skill seems counterproductive (especially when you have a lot of skills), but I haven't used SOs so I wouldn't know I guess, unless im just doing something terribly wrong.
    However, what exactly did you mean by writing a method in the SO to operate them? Like create a button that adds the effects to the list? I was trying to avoid that since the UnityEvent referencing behind the effects is somewhat convoluted, and I'm not familiar with SOs so I don't quite know where to start.
     
  8. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    You need to create only one SO for each type of effect, like visual, audio, damage over time, direct damage, the rest is parameters.
     
  9. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Wouldn't two skills that do different amounts of damage have two different SOs?

    I'm sorry if there's something that's generally obvious that I'm not picking up on
     
  10. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    You make two instances of the SO with a different value for the parameter damage.

    Look around 5:50 on this video:

     
    Last edited: Jul 16, 2022
  11. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Oh sorry, but thats not really what I'm asking. I think a better way to put my question is to say if there is a better way to package related scriptableobject assets/scripts together. I know the obvious answer is to "make a folder" but that would make... a lot of extra folders if there are say, 100 skills (meaning 100 folders to package the 4-5 separate SOs that belong to each skill) and dragging them into a reference sounds like a lot of extra work.

    If there isn't a straightforward way to do this, thats fine, I'll try to figure something else out.
     
    Last edited: Jul 17, 2022
  12. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    ScriptableObjects do not contain components. Only GameObjects do.
     
  13. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Well if you have 100 skills, then you should have 1 SO asset specific to that skill not more. If you SO refers to let say 5 other SOs it's fine, you can do that it is shown in the video I linked above (around 7:30). And then you have 1 folder for the skills and 1 folder for each of the 5 types of SO that makes 6 folders max.
     
  14. sliptrixx

    sliptrixx

    Joined:
    Mar 24, 2016
    Posts:
    12