- Home /
Question by
MelonHeadStudios · Dec 14, 2012 at 08:31 PM ·
objectsfindall
FindObjectsOfType Problems
I am needing (in order to make a pause menu) to use the unity function FindObjectsOfType function to find all the scripts in the scene NOT named "MenuButtons" to be disabled, how do I do that?
Comment
Answer by gabs · Dec 14, 2012 at 08:48 PM
I got to this solution (C#):
Well, make sure you're importing Linq first:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
The function:
void FindAll()
{
GameObject[] allGameObjects = GameObject.FindSceneObjectsOfType(typeof(GameObject)) as GameObject[];
var objsOfInterest = from obj in allGameObjects where obj.GetComponent<MenuButtons>() == null select obj;
if(objsOfInterest.Count() < 1)
return;
Debug.Log(objsOfInterest.Count());
foreach(GameObject go in objsOfInterest)
{
// do something
}
}
if you want only the root objects (their transforms have no parents): var objsOfInterest = from obj in allGameObjects where obj.GetComponent() == null && obj.transform.parent == null select obj;
I don't really think that the best way to pause a game is by getting every object that's not part of the menu, and do something with it, though. If you don't ave hundreds of GameObjects on your scene, this might be not so heavy.