Skip to main content
2 of 2
added 5 characters in body
starikcetin
  • 5.3k
  • 4
  • 26
  • 60

Cache all objects at the start and detect the changes in the array (or the list).

To elaborate more, have a list like this:

//This list holds all the GameObjects that are currently in the game.
private List<GameObject> _existingObjects = new List<GameObject>();

And call this method at the beginning of the game, and every time a scene is loaded:

//using System.Linq;

public List<GameObject> FindNewGameObjects()
{
    //Get all GameObjects.
    var allObjects = (FindObjectsOfType(typeof(GameObject)) as GameObject[]).ToList();

    //Substract the existing GameObjects from all GameObjects, so the result will be the new ones.
    var result = allObjects.Except(_existingObjects).ToList();

    //Update our list.
    _existingObjects = allObjects;

    //Return the result.
    return result;
}

The return of the FindNewGameObjects() method will be the list of GameObjects that came with the loaded scene.

Note: The code is not tested.

starikcetin
  • 5.3k
  • 4
  • 26
  • 60