I have an array of game objects in the GameWorld and they can get removed from that world. The problem is that some game objects have references to other game objects. E.g. Player class has a reference to a Bird. Bird gets randomly removed from GameWorld, but Player still has a reference to it. I currently do a null check to check whether the GameObject is still valid and in the world. However, removing an object from the array does not make that reference null. So how can I make it null?
Here is an example:
// GameWorld creates bird
ArrayList<Object> gameObjects = new ArrayList<>();
Object bird = new Object();
gameObjects.add(bird);
// Player references it
Object referencedBird = gameObjects.get(0);
// later in GameWorld, another scope, there is no access to the 'bird' object, trying to remove the bird from the world
Object objectToRemove = gameObjects.get(0);
gameObjects.remove(0);
objectToRemove = null;
// back in the Player class
Debug.log("is null " + (referencedBird == null)); // false! I need it to be true