In this tutorial you’ll learn how to create and use Scriptable Objects in Unity. Scriptable Objects in Unity can increase your workflow, reduce memory usage, and even decouple your code architecture.
Using Scriptable Objects to hold Item Data seems like the right way for me at the moment, but your solution is too complicated in my opinion.
Adding an event and eventlistener for each sword seems unnecessary to me, at least if you only want the information, which one was selected last. What are you gonna do if you have many more items?
I can think of 2 easier options right now:
You create specific GameEvent and GameEventListener classes for the swordData type, where you either assign a field or property which the Listeners can get as soon as they are called. Or you pass the sword reference like an EventArgument to your event and from there to your listeners.
Create a Scriptable Object, which just stores a reference to your last selected sword data item. This will be attached to every sword and the reference will be set set in your OnMouseDown bevore the Event.Raise(). Listeners will also have the refence object attached and look it up when called.
With both methods you would only have 1 GameEvent and 1 Listener component per Listener object.
Thanks for your interest in the tutorial. I especially like your second suggestion that utilizes an additional Scriptable Object to keep track of the last sword selected. If there’s one thing truly awesome about programming, it’s having a hundred different ways to solve a problem. This being a tutorial, I really wanted to drive home creating Scriptable Objects, and utilizing Unity Events within a Scriptable Object.
Thanks for this, I was thinking the tutorial was not very scalable. I tried out option 2 and it is a lot nicer than having to create loads of event listeners and game events for 100’s of shop items.
I just want to say that I love your tutorials. They are really well done and are super easy to follow. I wish there were more tutorial sites like yours. It really makes learning a new skill easy and fun.