Skip to main content
added 5 characters in body
Source Link
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 holdholds all the gameobjectsGameObjects 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 gameobjectsGameObjects.
    var allObjects = (FindObjectsOfType(typeof(GameObject)) as GameObject[]).ToList();

    //Substract the existing gameobjectsGameObjects from all gameobjectsGameObjects, so the result will be the new ones.
    var result = allObjects.Except(existingObjects_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.

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 hold 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 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.

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.

Source Link
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 hold 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 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.