Ways to destroy objects and why

  • Hey Guys, so I am in the sci-fi part of the Ultimate Guide tutorial... And I was in the part where Jon tells you to try to destroy the hitMarker right?


    But I just got a bit confused like... how do you define the best way to destroy something??

    For example in the galaxy shooter you destroy things immediately with Destroy(Name.GameObject); and thats all fine and dandy. Or you add a delay, cool.


    But like when do you need to use Coroutines to delete something for example, versus creating a new script saying just delete me? Or when do you have to cast the game object?


    Jon gives a quick explanation for this but I am having a hard time understanding why and when...


    I am still very unsure when I need to create a new script versus when do i try to do something in an existing script? And which one is more optimal for performance?


    Like I was watching some videos on scriptable objects for example and ppl that use those are big advocates for keeping every scripting behaviour separate because its easy to reuse things if they have very simple actions in them.


    I am a bit confused with all this, probably because I am very new to scripting...


    But any comments and thoughts would help a lot.
    Thank you!

  • I am also relatively new to scripting, but here's my take on it - and it really comes down to personal preference. The same thing can be implemented in many ways. Some courses like this one don't focus on elegant coding - but more on "flying on the cuff". It's great while you are still trying to understand what can be done, as well as reinforcing what you might have learned in a different course. I think I know the other course you are referring too, and there the Instructor does focus on elegant scripting, but it takes a bit longer to do. From yet other courses they refer to concepts such as clean coding and code smells. The great thing about all this is that you can see how different instructors approach the same topic, allowing you to take the best of each. So when it comes to multiple scripts or having all your programming in a few scripts it's kind of up to you. I have done courses where the Instructor even goes as far as having a separate script for each method - i.e. no script has custom methods. That's the one extreme. The other extreme is having everything in one script. Find what works for you - but the main goal is to achieve a readable project that you can quickly pick up on if you had to leave it for several months.

  • As to destroying objects until you have a lot more experience or move to a much larger game I would not worry about it. The different methods tend to come into play when you start dealing with performance cases that are more rare to reach. (Bonus if you want, look up Object Pooling, Jon had a 3 part tutorial on this site that talks about it).


    As to multiple scripts there is also no right answer. I am unsure exactly what is underneath Unity's hood but my guess is there is little difference between one script vs twenty. I have done a lot of coding in a professional setting and I can tell you that while code re-use is important it can be taken to extremes that cost you time in the long run. I have written code "that may be reused" but it never is, and likely never would be. I am more concerned about re-use within my own game/project.