1

Each enemy in my game is a Scriptable Object (Enemy1, Enemy2, etc). Each enemy also has its own unique script to determine how battles go (BattleEvent1, BattleEvent2, etc)

The way my logic currently works is:

  1. BattleSystem component has all BattleEvents attached to it.
  2. When battle starts, check the name of the current Enemy, and use that string reference to pick which BattleEvent to run (LOTS of if statements).

But this is bad because it's all string references, so I can't easily ensure that each enemy has a BattleEvent, and there's a lot of if/else statements.

What I wanted to do was attach each BattleEvent script to its respective Enemy Scriptable Object.

enter image description here

But I'm not able to drag the scripts in that Battle Event SerializeField there.

I would ideally like my logic to work like:

  1. BattleSystem loads the enemy, and just checks the Enemy.BattleEvent to load the proper script.

Since I can't attach scripts to the SOs, I can't get this to work. Is there any way I can achieve a logic flow like this?

damon
  • 2,687
  • 8
  • 25
  • 35
  • 2
    You can't have **scene references** in assets. You can however assign the reference on runtime! See my post [here](https://stackoverflow.com/a/60408573/7111561) where I describe how to use a provider and consumer pattern via ScriptableObjects – derHugo Jul 03 '21 at 17:23
  • Thanks for the link. Perhaps I need to do more research on the provider/consumer pattern, but I wasn't sure how to relate your answer to the issue described above. But what I think I've realized since I posted this is, the question I really needed to ask is "Can you assign an arbitrary script as a SerializeField?". If I'm understanding your post, you are only assigning basic data types to the objects, so I think there is still my issue of not being able to associate scripts to SOs, not regular variables. That's assuming I didn't completely misunderstand your post! – damon Jul 04 '21 at 17:05
  • You can assign to the SO whatever you want ;) also e.g. Dictionary and other non-serialized values. You won't be able to do so in edit mode but only on runtime though. Basically the same as if you would use a static class .. but with SO you are more flexible and can use multiple ones of the se types without having conflicts .. as you can see in my post I also used it to pass on a `Transform` reference as well as a complete custom class instance containing a Dictionary – derHugo Jul 04 '21 at 18:33

0 Answers0