Getting game objects in scene with the same type.

Hello everyone !
Let me introduce my problem to you , so i have script called “MonaBotBaby.cs” in my game. I am trying to get all objects in scene that has “MonaBotBaby.cs” attached.

Well , i don’t want to get only monabots i also want get other enemies and i created a simple function for this task like this ;

public static GameObject[] GetSameTypeObjectsInScene( System.Type objType )
{
        GameObject[] objs = GameObject.FindObjectsOfType( objType ) as GameObject[];
        return ( objs );
}

Simply it tries to get all object with given type and returns them as game object.

And i am trying to use that function in my other file called “EnemyManager.cs” like this ;

private void GetMonabotsInScene()
{
        GameObject[] monabots = ObjectManager.GetSameTypeObjectsInScene( typeof( MonaBotBaby ) );
}

But it is not working , it’s always says null array , reference and yes i am calling this function on Start() callback.

Any help is great , thanks :slight_smile:

Hi everyone again haha :slight_smile:
I fixed this simple problem. Here i will post the changes i made , so i hope it can help someone who have the same issue just like this.

public static T[] GetSameTypeObjectsInScene< T >()
{
        T[] objs = GameObject.FindObjectsOfType( typeof( T ) ) as T[];
        return ( objs );
}
private void GetMonabotsInScene()
{
        MonaBotBaby[] tmp = ObjectManager.GetSameTypeObjectsInScene< MonaBotBaby >();
}