unity get all gameobjects with component code example

Example 1: unity find all objects with script

public void GetAllObjectsWithScript() {                
     ScriptName[] list = FindObjectsOfType(typeof(ScriptName)) as ScriptName[];
     foreach(ScriptName obj in list) {             
     	// Do whatever          
     }     
}

Example 2: get all the game objects in a scene unity

Resources.FindObjectsOfTypeAll(typeof(urFavClass));

Example 3: unity get all gameobjects

//Get's all gameobjects and puts it in a list called "GameobjectList"
Object[] GameobjectList = Resources.FindObjectsOfTypeAll(typeof(GameObject));

Tags:

Misc Example