i’ve struggled with a lot over the past few days…actually still a few battles in progress i haven’t yet won.
i want to focus on something good, the sort of ah-ha moment i had sometime after watching a tutorial video mimicking the Mario Odyssey kingdom select menu. while i still struggle to comprehend the entire project, something clicked.
it happened when i was creating a health pickup to heal the player. speaking of that, i was getting this weird bug/issue, where when I create a new animation clip, the editor freezes. well, not so much freezes, but locks up. i can hover my mouse over buttons and they highlight, but i cannot click any of them. luckily, restarting Unity seems to work around the issue and the created clip is there when the editor reloads. still, its quite annoying when i was to setup several simple clips.
anyways, the ah-ha moment! i’ve felt i have been struggling to really understand object-oriented programming in general, but something clicked when i was making a health powerup. i wanted to create a sort of loot system and what i made is so simple and effective.
i realized what i needed, an item or items, and a percentage chance to receive that item. so, i created a public class called DroppedItems, with no deriving from Monobehavior, having two public member variables: a GameObject called name, and a float called dropChance with a [Range(0,100)] limiter. this, along with the entire class having [System.Serializeable] at the top allow me to access the values in inspector, i believe.
in another class in that same script, this time deriving from Monobehavior, is where i do all my working and figuring. first, i create a public List with type of my brand new class i just created, DroppedItems. this allows me to populate the List with gameobjects by dragging and dropping item prefabs and set individual drop chance percentages via a slider in the inspector!
then i have two functions, DropItems(), and then SelectItem() which returns a GameObject. DropItems() is a foreach that loops through the List instantiating the items, and the item drop chsnces are determined by the SelectItem() and the float value.