Find GameObjects with a certain Script [Solved]

I know tags would probably be an easier way to search but I’m using them for other things and I basically want to create a list GameObject or Transform containing all object with a specific script called “Stat”. I have tired to use FindObjectsOfType but that creates Object and I can’t seem to find a way to convert object to transform or gameObject. I am posting the script that I’m trying to use to do this followed by the script that the first script is searching for. But first here is Snippet because this is a really long line of code. But first here is Snippet with the code in question because this is a really long line of code.

public void AddAllEnemies(List<Transform> stuff)
    	{
    		//GameObject[] whatever = GameObject.FindGameObjectsWithTag("targetTag");
    		//Object[] whatever = GameObject.FindObjectsOfType(typeof(Stat));
    		GameObject[] whatever = FindObjectsOfType(typeof(Stat));
    		
    		foreach(GameObject enemy in whatever)
    		{
    			AddTarget(enemy.transform, stuff);
    		}
    	}

The answer was:

	public void AddAllEnemies(List<Transform> stuff)
	{
		//GameObject[] whatever = GameObject.FindGameObjectsWithTag("targetTag");
		//Object[] whatever = GameObject.FindObjectsOfType(typeof(Stat));
		Stat[] whatever = FindObjectsOfType(typeof(Stat)) as Stat[];
		
		foreach(Stat enemy in whatever)
		{
			AddTarget(enemy.transform, stuff);
		}
	}

Well FindObjectsOfType was the right answer. You can do something like this

var myScript : ScriptName = GameObject.FindObjectsOfType(typeof(ScriptName)) as ScriptName;

for (var aScript : ScriptName in myScript)
{
//do whatever you want to
}

This will give you all the scripts of it’s kind in the scene. If you are look for a script named “stat” then just replace “ScriptName” with “stat”.

I didn’t read through your code as it was kind of too long :wink: So I hope I got your problem anyways