Context
This is for an Inventory System, and I’m trying to make items the more automatically-ish way possible.
This is an issue regarding Weapons spawning bullets. Sort of.
Let’s say I have a ScriptableObject called ItemData. It is a pretty generic ScriptableObject, one that holds only data.
Now, let’s say that I have it assigned to a Script called Item. Also, let’s say that I have another ScriptableObject assigned as one of the various ItemData’s fields. This one is called MWActions. It is a slightly more complex SO, containing not only data, but Methods as well. It inherits another SO which is called WeaponActions, which inherits Actions which inherits ScriptableObject.
Now, as needed, I have yet another ScriptableObject assigned to this MWAction one. Lets call it… ProjectileData.
Just to recap, this “assigned var on assigned var” goes like this:
ProjectileData is assigned to MWActions which is assigned to ItemData, which finally is assigned to Item, the caller MonoBehaviour.
I also have a Script on my player that is used as an intermediate between the visual part of the items (i.e. being displayed on the player’s hand, or the position of the bullets depending on each item). Lets call it CharacterInventory.
Issue
Now, I’m trying to pass the reference of the ProjectileData to the CharacterInventory from the MWAction ScriptableObject.
The player selects the weapon and clicks to fire. The MWAction “assigns” (or sets) the projectileData variable inside CharacterInventory and spawns a bullet.
The bullet was supposed to get that ProjectileData and use it, but unfortunatelly that won’t happen. The projectileData var is null by then.
The issue is, after this “assignment” method finishes running, the reference on that other Script becomes null. Now, why is this even a thing? Is it a bug?
Some Notes
The reference on the other script keeps true to the PData passed to it until the method finishes. It only gets null after that.
The assignment isn’t the only thing happenning on that method, but nothing breaks the reference to the PData.
That crazy inheritance above is needed because I’m trying to make use of the very flexible Polymorphism Serialization that Scriptable Objects have. I’ve already tried with MonoBehaviours, but… No success.