How to get all GameObjects wit any script attached to it

Hello all,

when I open my scene, I get this error:
The referenced script (Unknown) on this Behavior is missing!

When I click on it, it doesn’t show me, which GameObject it is referring to. As I have a lot of GameObjects in the scene, it would take me forever to go through it manually.
So I would like to write a script, which goes through all GameObjects and returns the ones with ANY Script attached. Unfortunately there seems to be no such a general script-component. So how do I do it?

This is how my script would look like in an ideal world:

GameObject[] allObjects = FindObjectsOfType<GameObject>();

foreach (Object obj in allObjects)

if (GameObject.FindObjectsOfType<Script>)
{
       Debug.Log(object.name);
}

But this is not possible, so how can I do it correctly?

Thank you
Huxi

Simple, use tag to every game object you wanted to be in list and then call Game object find object with tag will get all in one place or there is 100s of way to do it. Let me know if that helps.

–

Just to make sure that I got it correctly, is your proposal to go to each GameObject and set a tag on it? As I mentioned, I have a lot of GOs.

–

2 Answers

2
ScriptName[] allScriptObjects = FindObjectsOfType<ScriptName>();

Hello @Nicolqs, but I don't want to search for a specific script, but get all GameObject with ANY script.

–

Search for MonoBehaviour instead of ScriptName

–

You might not have to write a script for it, all you have to do is write t:ScriptName to search bar in hierarchy window. (This doesn’t work for components in prefabs in Project view, but does for ScriptableObject or any other asset type). The hierarchy window will then only show gameObjects with a component of ScriptName type.

Hello @pangamini, but I don't want to search for a specific script, but get all GameObject with ANY script.

–

Search for common base type then. MonoBehaviour, your own base script that others extend, an interface...

–