I have 100 weapons for a player. I want to change the weapon’s object on the player’s hand according to the current weapon index. I thought of two methods to handle this.
Method 1
Add all the weapon objects to the player, set active for the current weapon and set inactive for other weapons.
Method 2
Add all the weapon’s prefabs to the script. Instantiate and Destroy the weapon object when changing weapons.
Don’t instantiate and destroy these are a big hits on performance especially with the garbage collector.
Method 1 is how i would do it, but create a weapon change system, where i would use a dictionary, and use index as the key value. Just like a pooling system, but for changing weapons.
Note:
All weapons should be deactivated, and only activate the newly selected weapon, and deactivate the previous selected weapon. It’s a waste of performance to loop through all weapons to deactivate them whenever you change to a new weapon.
If you research object pooling you will see that they only use setActive method and say that instantiate is worth than setactive for cpu efficiency. but in this case remember that you put all weapons to ram. so method 1 efficiency for cpu. that prevent lagging in game. and method 2 is efficiency for ram usage… but nobody care ram usage that much when they make a game. because lagging is related with cpu mostly.
Edit: dictionaries slower than native arrays. so i wouldn’t prefer to use them for such a system