[Question] what is the best way to manage logic of collectable objects?

Hi, I have a question. I put the case, I have many coins (each with particles and sound) and I do not really know that it would be more optimal.

1 - put each coin its particle and sound and a script that is active in the OnTriggerEnter2D to activate the particles and sound.

2- Put it in the player class and add a particle of the coin and sound and every time I pick up a coin active fx from the player.

3- Same as 2, but in a GameManager class or something like that

I think the most optimal is 3 not overload with such particles and sounds. and the second having the particles and sounds of the coins in class player… have them imply any “collectable” object should go there which would make the player class crazy.

I would like to know your opinioin and how to solve this little dilemma

Thank you very much.

Think modularity. Your coins should take care of everything coin related. Your player everything player related.

In most cases this means coin sounds and effects belong to the coin.

1 Like

Yep, though this doesn’t mean that they can’t work via a shared pool. So you might in actuality have 100 coins, but only 10 coin particles and 10 coin audio sources. Whenever a coin is picked up it asks the pool for the next unused particle and audio source, does its thing, then returns it to the pool.

Don’t worry about the pooling unless you need it, though.

1 Like

@Kiwasi
normally do that but talking hundreds of coins, not whether it is necessary to have in each currency a sound and a particle that perhaps neither is activated.

@angrypenguin
yeah i’m thinking in something like that, maybe a poolManager how control all request for new spawns.

thanks to both for your answers :slight_smile:

1 Like

Your concern about having hindered a of sounds is unfounded. Each coin has a reference to a single audio file, or a list of them. Each coin has a reference to a particle effect prefab, and instantiated it at runtime if needed. References are cheap.

Pooling is a separate concept, and is used to reduce the cost of memory allocation and deallocation. In all but the most demanding platforms pooling only makes sense if you are constantly destroying and creating objects.

1 Like

Or if you want to avoid GC allocation which can, unfortunately, matter anywhere.

1 Like

yes but the question is what is better have 100 coins with 100 particleSystem in the scene or 100 coins and 10 ParticleSystem and activate it when is needed.
or in other words what is the cost of the two options… i dont have a pro version to test with the profile or something but a test with 1000 coins with sounds and particles with and without pool was great xD

If it was “great” with 10 times more coins than you want then the answer about which approach is better is: “it doesn’t matter”. Obviously they’re both good enough, and the one you tested is already done, so stick with that and move on to the next thing.

The answer might change later, but that’s ok. Unless you’re able to properly plan ahead then you’re better off taking simple, maintainable approaches and only making things more complex if and when you need to optimize them later.

As for how you do that planning ahead, you test each proposed solution on your target platform and go with the one that works best. Guessing is just about useless.

You are still not getting the concepts of references and prefabs. You can have 100 coins with one particle effect.

at this point i’m confused

i thought that every time i call the play method of the particle internally unity instantiate a clone of my reference, and if i call the play method 10 times it will create 10 clones of my references.

But if i understand you, you are saying that is the same reference and doesnt have any overload in the system to call the play method for example.

is that? every day learns something new :stuck_out_tongue:

and thank for your patience :slight_smile:

You are correct. However if you don’t call the play method, nothing is created.

There’s a distinction between resources and objects that use resources. 1000 game objects using the same mesh and texture does not load the mesh and texture 1000 times. The mesh and texture are loaded once, then 1000 GameObjects and their Components reference the same mesh and texture. The GameObjects are far smaller, so you can have loads of them. They do have their own overheads, of course, but they’re not as steep as you seem to expect - especially when they’re sitting dormant.

this may be elemental unity question
this is why is important the use of prefabs? because you are loading a reference of the original and dont have the same overhead that create X instances of the same object.

No, prefabs have nothing to do with using resources differently. They’re just a way to save a GameObject’s configuration outside of a scene. Using a prefab is exactly the same as copying any other GameObject.