- Home /
Right way of controlling objects in Scene?
Hello there, I often use a script, which sits on things like ParticleSystems of exploding bullets and destroys it after a certain amount of time. The object (like the bullet) doent exist anymore to destroy the particle system.
Is it commen to do so? or is there a better way of handling such things?
I hope you understand me :D
Tanks for reading and have a nice Day! (btw: sorry for any misstakes! :D)
Depending on how often you need bullets, particles, etc you could go with object pooling. You would no longer destroy the objects but ins$$anonymous$$d just use them when you need them(turn them off if you aren't using them and turn them on and reset them when you do).
Decent tutorial for you if you would like. https://www.youtube.com/watch?v=LhqP3EghQ-Q
If that really isn't what you are looking for. You could create a manager that handles the instantiation, deletion, etc of all your objects that you need. So ins$$anonymous$$d of having the bullet tell the particle it needs to delete, you would have some trigger or something in your scene that tells the manager what objects it needs to delete. There are an infinite amount of ways that you can do what you would like, those are just a couple.
Oh, thanks alot! I will try it later on.. ;) I used an idea based on your tips.. it helped alot! :D
Answer by NoobGaming · Jan 22, 2018 at 10:20 PM
Hey there. I would make the particle system a child object of the bullet and once the bullet hits something then play the particle system. Then once it's played its explosion animation then destroy it. That way its not trying to get something that isn't there.
But as for a better way would be object pooling. That way there is no destroying and filling up the garbage collector. This way you reuse gameObjects.
Watch this: https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
Thanks, I now use the reusung way and it made everything a lot simpler... Thanks alot!
but I already have another question :D : I have 2 scripts for 2 types of enemys, one is shooting all the time in the same direction and the other one is tracking the player. They both are not very long and use the same prinziple. I could combine them and use a bool to swich the modes. Is it commen to use one script? or is it better to leave it as is?
(I am often quite confused on how to do such things.. :) )
Short answer: it is better to leave them separated.
Long answer: you could create a class structure where the common behaviour is in the base class, and the specific behaviour (shooting vs. tracking) is in derived classes. Or even better, use the strategy design pattern and inject a behaviour object into a generic enemy class (keywords: strategy pattern, dependency injection).
Ok... it sounds a bit too complicated.. :D thanks alot and have a nice day :)
@WbrJr Your welcome! Always happy to help. For the other question, separate is better for the fact it stays cleaner and easier to read and know what does what. Expecially if the scripts grow in complexities. But to be honest if your the only one working on the game do what works best for you and your coding abilities.
Best of luck on your game!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Destroy specific Objects 2 Answers