[ad_1]
One of the best ways that I got here up with is, in accordance with the observer sample, to inform all lessons the place the article was saved about its destruction and take away references to it from there.
That is what I at all times do.
[System.Serializable] public class DestroyableEvent : UnityEvent<Destroyable> {}
public class Destroyable : MonoBehaviour {
[SerializeField] personal DestroyableEvent destroyedEvent = new DestroyableEvent();
public DestroyableEvent DestroyedEvent => destroyedEvent;
void OnDestroy() {
destroyedEvent.Invoke(this);
}
}
Then in another class you’ve one thing like this:
[SerializeField] personal Destroyable prefab;
personal Checklist<Destroyable> objects = new Checklist<Destroyable>();
public void SpawnObject() {
var newObject = Instantiate(prefab);
newObject.DestroyedEvent.AddListener(objects.Take away);
objects.Add(newObject);
}
If objects are created and destroyed ceaselessly, it’s best to use a pooling system as a substitute, however you continue to want occasions to trace when an object is returned to the pool so you possibly can take away it from any related lists.
One other resolution you should use is to periodically scan lists for null entries and take away them, or take away them while you attempt to entry an entry within the record and decide that it is null. I desire to make use of an event-driven method as proven above.
[ad_2]