Flexible Pickup / Collectible System in Unity With Events
ฝัง
- เผยแพร่เมื่อ 8 ก.พ. 2025
- Almost every game has some sort of collectible or pickup system - the player runs into the item and picks it up. But many people struggle to setup a system thats flexible to handle a whole bunch of different types to collect without getting their code all tangled up.
In this Unity Tutorial we'll cover how to use a simple interface and events to setup a flexible collection system that lets you handle and respond to all of the different types of items your game has to collect.
Maybe the items go into your inventory, maybe they get logged in an achievement system, maybe they all want to play a sound and play an animation, with this architecture you'll be able to setup all of that incredibly fast.
Give it a try and let me know what you think, its not perfect, but its very powerful.
✨Want to support the channel?
Buy me a coffee ☕: ko-fi.com/bmoli
➤SUBSCRIBE to become a big handsome billionaire, probably
➤LIKE the video if you enjoyed, it really helps the channel!
➤Join our DISCORD SERVER to become a god amongst men: / discord
Thanks for watching!
#bmo #unity #tutorial
Great tutorial. Full of solid programming principles that I really appreciate, especially given how easy to understand your videos are. One thing I'd like to point out is that I temporarily got tripped up because I don't think your video says which game object to attach the "Collector" script to. So at first I had it attached to the coin and it took a minute to realize that it only works correctly if the player has the "Collector" so that the collision is detecting the right game object. But thank you for making such a helpful video.
It's awesome. Our codes are spaghetti. This is very clean and SOLID and DRY code. Thanks and please more like this.
This is really insightful!
This was a very informative video! I am going to have to learn more about Interfaces, and Events but this is right up the alley I am trying to put in my game!
Please keep up the great work, your method of explanation and no nonsense approach is extremely helpful
I got you
Interesting! I've never heard of interface before. It looks really useful. Thanks. :)
Theyre pretty important!
@@BMoDev I'm following a tutorial and he is using interfaces. Since you made that video, I can understand what he's doing. :)
Yours are the only tutorials I don't feel the need to play at incresed speed to avoid "downtime". That says a lot about how well-produced and well-paced they are.
Thank you for making these widepeepoHappy
The only think keeping this tutorial from being a solid 11/10 is the lack of timestamps.
Thanks man, what I was looking for and to the point.
Glad it could help!
Legend
Excellent tutorial, thanks!
FYI you can use properties instead of fields in interfaces if you really need them :)
Thanks This Really helped 👌👍
You have a class specific to a Coin but that would mean having a different script for every object you want to be a collectable as opposed to having a single script that you can then use on any collectable item and then just add a reference to a scriptable object which tells the collector what item it is..
You are right in terms of " the same wotk with every collectable". But if you need to have different logic on collect, then it would be much harder to determine logic that works with general Type and just using SO as type
Finally a tutorial that doesn't use scriptable objects for inventory/pickups. Funnily enough I've just been working through an ICollectable version of this before watching your video, love interfaces. I'm glad to see I'm on the right track 😁
It's really refreshing to see interfaces taught so well in such a bite sized chunk of information 👌 nice
is it bad if i dont use a scriptable object for collectables?
@LostSol25 I don't think it's bad at all.
At the time I made this comment I was working on something simple and really thought this was a good tutorial for it.
With scriptable objects it's entirely down to the game your making and how deep the rabbit hole goes. You could still use an icollectable with a scriptable object item if you needed, although there are other ways.
@@friedcrumpets Ohhh makes sense, thanks for getting back!
Cool vid! 🔥🔥🔥
How to i save the coin counter over different scenes
Amazing thanks!
Mine doesnt debug at all , how do I fix it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collector : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
ICollectible Collection = collision.GetComponent();
if(Collection != null)
{
Collection.Collect();
}
}
}
I did the exact same but not working
Edited : Okay just realised that the collector script was suppose to be attached to the player.
Thank you, i had the same problem
@@bastienbaivier3062 np mate. It gets A little confusing at a later stage when implementing the other codes for your enemies, AI, and so on
You can also do on the trigger enter method:
if(TryGetComponent(out ICollectible icollectible)) { icollectible.Collect() }
I would trade in all my coins for this vid 💰Money my dude!
in the Icollectiable class it says the modifier public is not available
Good stuff, although worth noting it's pointless to have interfaces if your dealing with just one or two. It makes code navigation more annoying / lengthier because of the detachments and any components working thorough interfaces quickly become welded as-is, since changing them could break every other consumer's assumption. But that goes hand-in-hand with interfaces 😅
Hey, thanks fro the tutorial but I have an issue : when I change scene in my game and come back, I can collect the collectable again, and I don't want that. Can you make a video about it please ?
Can you make a part 2 with how to save and load these collectibles ?
What is a method and delegate
Can i use this on 3d??
How to add different values to different gameobjects and then add them together. For example 1 gameobject worth 2 point and the other is worth 4. And if i collect, for example 10-10pcs of them, then the result is: X Thank you in advance.
What if my loot is a scriptable object like in other videos?
What's your opinion the Collector doing the OnTriggerEnter2D vs the Collectible?
Hey ! Were do you find binding of isaac sprite please ?
watched around 3 videos of events and still can't understand it. any tips ?
Try them yourself! Theyre way easier than it seems. I really struggled when i was first introduced to them
@@BMoDev also I have to say that u're so lovely and I like you🌹
how to trigger the collector script is it on the player or on the event system because when I try to collide with coin I'm getting no log message there is rigid on the player
So I'm a bit confused. How would I make what I would call a collectable shelf. A place in game you can go to and look at what you've collected. This would work right, if I understood the video correctly
Hiiiii! I'm so very curious how can I make an AR element collectible on tap? Is it possible? I searched the whole internet for a tutorial but no luck :(
The colliders aren't working for me. I set the OnTriggerEnter method on my pre-existing player script, but nothing actually happens when I touch my collectible. I'm using Unity 2020.3.37f1.
Edit: I found a fix. My player object didn't have a Rigidbody. I don't know why it needs one for the trigger to work, but it works now.
even my player has a rigidbody it didnt collect the collectible
@@geoClexE Collector script goes on player and add boxcollider2d to player
@@williamr578 This answer was gold! I was thinking, "Don't we need to attach the collector somewhere?" Thank You!
@@williamr578 Thank you! After 30 minutes of finding which my fault by missing some code then i saw this.
@@williamr578 u are the boss thank you !!
Ive spent 2 hours wondering why I do not get the "collected coin" debug message in console when i realised I never assigned the "collector" to my player.... lmao
Naisu
My soul wants to understand, but my brain is like nope.
I get interfaces, I get that you showed 2 ways of implementing them, there's no reason not to use both depending on the use case.
For example I tend to make my AI and Player inherit from the same place. I can just put everything on that and define thing there then the player and AI are now damagable, or whatever.
But UnityEvents and System Actions... man... The worst part is I need to get my brain to accept whatever the hell is going on here because I want to make an inventory system. The guides I follow tend to use events but I just can't get my brain around invoking them... it's like I sort of know what they do and how they do them but I can't process when or why to use them.
Alright a good way to get this thing to target a specific inventory is to use the collider on the item. Through the itempickup script use a OnTriggerEnter to find the target that entered and find if it has an inventory. To do this, you'll have to delete the OnTriggerEnter check on the player and make a new one on the ItemPickUp.
After that I would make the inventory a child of the player and then do a GetComponentInChildren(); So...
//Inside of Trigger Enter:
var inventory = other.GetComponentInChildren();
if(inventory != null) Collect(inventory);
Collect is changed to this:
public void Collect(Inventory inventory)
{
inventory.Add(item);
Destroy(gameObject);
}
Another extension to this may be to add multiple items at once, so say one of those blue gems is actually a stack of 5, or what if that was the case but you could only hold 3 blue gems.
Okay yeah, tested the above and it worked, the only thing is that I get a null reference for the DebugLog on the first check of the dictionary. Not sure why, but commenting it out and the system worked perfect. I can now specify the inventory I want an item to go to on pick up. The downside is the the Events, Delegates, and Interface were mooted by just putting a ontriggerenter check on the pickup (Gem) script.
But this is to the point of my original post, see I can do something like this and suddenly script from 3 or 4 different classes (1 interface) are gone. In their place is 3 lines of code that all exist on a pick up script.
This is why my brain is like nope, whenever I ask if this is a good time to try using Events.
@@dibaterman Thanks for the update.
@@wmetz1869 No problem, a piece of advice is look at Inventory as being 2 different things.
1. Is Game Design the other is Just making a database.
If you can make a database fine, now it's time to connect the information in that database to the game in a meaningful way.
To do this you create slots that look at the database and makes a clone of that item in your database to the slot. If you have stacks, then make a int for the slot and have the slot grab the int from the database.
That sound simple until you ask how do you get the slots?
He shows you in this video a very easy way to do this: "Foreach(Transform child in InventoryPanel) is a fantastic way of populating the slots in an inventory.
yeah my brain not enough for understand
First
🥦🎮⚡⚡⚡⚡🙈