- Home /
Get all other GameObjects (not the one script is attached to)
The question says it all. I need to get all other gameObjects of a tag (which i did) then exclude the gameobject in which the script is attached.
I tried
if(cars[i] != transform.gameObject)
then do what i want to do. (which isnt exactly excluding it from the array, but not using it) but this doesnt work.
Answer by dmg0600 · Sep 19, 2014 at 03:32 PM
List<GameObject> cars = new List<GameObject>(GameObject.FindGameObjectsWithTag("Tag"));
cars.Remove(this.gameObject);
This way you will get all the GameObjects with a certain tag and exclude the GameObject that has this script.
Answer by betaFlux · Sep 19, 2014 at 02:59 PM
May be this will work:
List<GameObject>() cars;
void Start()
{
cars = new List<GameObject>();
}
foreach(GameObject car in GameObject.FindGameObjectsWithTag("YourTag"))
{
if(!car.GetComponent<ScriptToExclude>()) // if the script is not on the gameobject...
{
cars.Add(car); // ...add it to the list
}
}
What you did is check if the script is attached to the cars. But unfortunately the same script is attached to all the cars, but i still want to get them. I just dont want the gameobject the script is attached to.
I want to exclude the current gameobject on which the script(the one which is supposed to be checking all those stuff) is attached
Your answer
Follow this Question
Related Questions
How to check if a specific gameobject is in the array? 1 Answer
selected object in array lost in translation 0 Answers
How to remove objects from a list ? 3 Answers
How do you save GameObject[]? 1 Answer
Can't add GameObjects to ArrayList 1 Answer