- Home /
How can I get every other GameObject except for the one I'm using?
Suppose I have a script attached to a GameObject (let's call it "my_object"). What I want to do is to call any other GameObject except for the one the script is attached to (in this case, my_object). It should be something like:
void Start () {
GameObject [] objList = GameObject.Find ("Any other object except for this one");
}
How can I do that without putting "my_object" inside the array?
why is it necessary that my_object is mot part of the list?
just get the gameobjects, iterate them and skip my_object
Because I want to compare my object with the rest. That means I don't want it to compare to itself.
Answer by ShadyProductions · Jun 24, 2018 at 06:47 PM
A gameobject has an InstanceID property that you can check on which is unique for each gameobject.
Make sure you're using System.Linq;
GameObject[] gameObjects = Object.FindObjectsOfType<GameObject>().Where(f => f.GetInstanceID() != gameObject.GetInstanceID()).ToArray();
Your answer
Follow this Question
Related Questions
how do I find components through variable 1 Answer
Loading function into array using GameObject.Find 1 Answer
Alternatives to GameObject.Find(); 1 Answer
Finding Children question 3 Answers
Will a large array of game objects slow my game down? 2 Answers