Item Stacking

When asking for help with an error, always make it clear exactly which line of code the error occurs on (if there is one). It’s also a good idea to post your code using code tags rather than screenshots.

A null reference exception is a common error that occurs when you try to make use of a variable that doesn’t have a valid value. It usually occurs when you write a period: myVariable.something will give an error if “myVariable” is null. Find the line that’s giving the error, find the periods on that line, figure out which one of the things just before a period doesn’t have a valid value.

The other error is because ScriptableObjects are special and you aren’t supposed to create them on your own; since they are a Unity type, you have to go through Unity to make more of them. It’s also pretty unusual to be creating more of them like you’re doing. Are you sure that (1) you really want Item to be a ScriptableObject, and (2) if so, you really want to create new instances of that class, rather than just passing around references to existing instances?

If it were me, I might use a ScriptableObject class to represent a sort of blueprint for items–each ScriptableObject representing a certain kind of item–but then use a MonoBehaviour or even a plain old C# class for each copy of that item. Like, if you find a stack of 20 minor healing potions, there would still be only one ScriptableObject that describes what a “minor healing potion” is, but the actual items-in-the-game would just have a shared reference to that ScriptableObject, rather than 20 copies of it.

1 Like