- Home /
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);
}
}
Total CODESTOR$$anonymous$$ man, no-one is going to look through this.
Please, strip out all the stuff that you $$anonymous$$NOW is not the issue and EDIT your question posting the updated code SNIPPETS
SNIPPETS!!!!
Had the same thought. This appears to be the area of concern:
//GameObject[] whatever = GameObject.FindGameObjectsWithTag("targetTag");
//Object[] whatever = GameObject.FindObjectsOfType(typeof(Stat));
GameObject[] whatever = FindObjectsOfType(typeof(Stat));
Thumbs up for getting to line 595 before SHOOTING YOURSELF :D
I have to be honest I was so close to rejecting this from the moderation queue :D Everyone deserves a chance, right? :D
Sorry I had past problems and the first thing everyone said was don't just give me part of the code. I need to see all of it to whatever. Oh well I will cut out the main problem and throw it on top top and leave the rest for the people who need everything.
Answer by fred_gds · Aug 31, 2013 at 01:07 AM
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 ;) So I hope I got your problem anyways
That was almost right but I was able to finish the rest myself. The last thing I need to do was change.
foreach(GameObject enemy in whatever)
{
AddTarget(enemy.transform, stuff);
}
to
foreach(Stat enemy in whatever)
{
AddTarget(enemy.transform, stuff);
}
but still thank you. :D
Your answer
Follow this Question
Related Questions
Trouble with targeting enemies from a List 1 Answer
Is there an easy way to add an array of raycasthit2Ds to a list? (javascript) 2 Answers
Is it possible to convert c# generic List<> to normal arrays? 1 Answer
Emptying a Generic List / Unexpected Behaviour 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers