How to properly DESTROY GameObject & REMOVE from the List in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to my channel today let's talk about how we properly destroy an object and remove its formulas for demonstration purpose I have an amazing package for RPG character controller and if you guys want to use data sets for your YouTube Project I will put the links down in the description below and you can find it on the asset store and in here I have a Basics character movement with support jumping and attacking something just like that I have an file NPC characters here and I can just use attack to destroy one of them right there so let's take a look at the course first in here at the ends of the attacks movement I will luxury enemy RPC list wish I will assign from this inspector and if the distance between sells element in the list and the quotations of the character is less than 2 I will destroy thus NPC and also Abu remove its own list and if it does I will lock our measures which call enemy and the enemy names is not near so or the first log nothing strange with that code is follow all the logic sets we have in our mind we look to the NPC list and if a distance of one of the character is less than two we will destroy it because destroy and game of shakes lesson guarantee that's at least that's in references we will also remove a formulas to avoid any missing reference exception now let's see at the editor and I will show you something definitely wrong with Disco in here at the RPC character I will I see enemy NBC list wait five NBC character over there okay let's just run so we come to the character and just punch it now you will see that one of the NBC characters has now been gone and we have four left however only three meshes has been free now not only that in our NPC list we still have four elements and something strange is us that is no error there's no running everything seems fine but somehow we have only three message free now it should mean four I will try to destroy another NPC now only two of the meshes is free now although we have three NPC here so what happened if we take a look back to intercode the reasons for this phenomenon to happen is we actually modify our list when we incrementally look to it because we remove the elements all the NBC Carter does come after us the one being destroyed the index of though had been shipped to the left one index that is why one of the log has not been printed I will ask some debug for you to see which of the index have been locked I will say the NBC in index I as mean destroy same has not been destroyed okay now let's see in letter you will see that the NBC index 0 1 and 3 has not been destroyed and then index number two is being destroyed but we haven't seen anything about index number four although we have five elements in our list the reason for that's because we have removed the elements of index number two so all the edits come up to is has been shipped to one index so index number trees has now been number two number four is now number three and number five is number four sorry let me say that again in this time because the index 2 have been destroyed the index number three now move to the index number two and the index number four now move to list number three to fix this issue we will correct and reverse for Loop four and another r copy the list actually I will save the code here for copying because follow Auto Logics remain the same but this time we're gonna loop from the last of the list to the begin and because we looping in dust order if an elements have been removed nothing on a chance for the rest of the elements now let's remove some of the law and see in editor as you can see now after we destroy an object we will have four meshes so the reasons I want to show you these techniques is because it is very easy to miss error you will see that says no Arrow says no warning everything seems fine and it is the card box is hard to debunk and when we try the reverse follow everything's now come back to normal the logics that we implemented has been correct that is the end of my video please like subscribe and thank you for watching
Info
Channel: NotJustUnity
Views: 2,779
Rating: undefined out of 5
Keywords: Unity, Game Development, Memory Management, Performance Optimization, List Management, Data Integrity, Code Efficiency, Unity Tips and Tricks, Best Practices, Unity Scripting, Game Development Workflow, Game Object References, Unity Components, Game Development Tutorial, Unity Performance Tips, Code Organization, Data Structure, List, Array, Collections, Programming, Data Management, Dynamic Lists, Array Operations, Unity Development, Data Storage, Algorithm
Id: pmSdcWTfdA4
Channel Id: undefined
Length: 8min 32sec (512 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.