How can i use a list too look for a specific value in some already declared Game Objects?

Updated on May 14, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on May 13, 2018

I have 9 GameObjects declared, each object has an ID variable. I need to loop through all of the objects and verify whether the ID is equal to another variable. I have been told that I need to use a List but:

List<GameObject> GObj = new List<GameObject>();
GObj.Add(A1);              //Done for each GameObject I have declared
foreach (GameObject ??? in GObj)
{
bool a, b;
 a = IsFree.CheckID(Position);  //this checks if a random integer is equal to the id of one of the game objects
/*/Every game object is an anchor with an id: if the random generated position is equal to one of the ID's
a cube (that resambles the enemy) is placed in the coordinates of the anchor/*/ 
b = IsFree.GetIsFree();  //this checks is the chosen position is free
if (a == true && b == true)
 {
 Cube.transform.position = ???.transform.position; //this should moove the cube in the chosen position
//Somecode
 }
}

What do i have to put in the “???” to make it go through all of the objects?

Any other Ideas to make it easier?

  • Liked by
Reply
0 on May 14, 2018
            List<GameObject> m_Objects = new List<GameObject>();
            int someID = 2;
            foreach (var obj in m_Objects)
            {
                if (obj.m_ID == someID)
                {
                    //Do Somthing
                }
            }

Not sure if that is what you are looking for. What it does is loops over the collection in the list and compares each of the values of the variable m_ID declared inside my class GameObject. If the value of m_ID is the same as someID we can do something. (This is not unity C#. You cannot modify the GameObject class in Unity.)

  • Liked by
Reply
Cancel