- Home /
Remove an element from an array
Basically I'm trying to let the player click on a button that was generated with information from an array. When the player clicks on the button, the button is destroyed, but, when the level is reloaded, the button is back because the information is still in the array. How can I remove that element from the array?
Answer by Orami · Jun 19, 2017 at 06:50 PM
Save what you have removed into some persistent data there are many ways to do this. Then read that data and remove any extra buttons or make the buttons persist through scene changes with doNotDestroyOnLoad() and just disable them when you don't want them to display or be interacted with.
You can find here an example of a Singleton (it's the technical name of the system that use DontDestroyOnLoad()) : https://unity3d.com/fr/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager
tell if it's helpful or not to you
Answer by bobisgod234 · Jun 21, 2017 at 01:27 AM
You can use a List instead of an array, which has a Remove() method. Arrays are supposed to be a fixed length.
Alternatively, if you really must to use an array:
myArray = myArray.Where(x=> x != objectToRemove).ToArray();
Make sure to add this to the top of your scripts:
using System.Linq;