- Home /
 
Obtaining an array of positions from an array of gameobjects
I need a list of positions from several gameObjects tagged as "Wall". I have searched all over, but only found specific answers to others' problems.
Would I use 'for()' or 'foreach()' here? In my LevelScript is:
 public Vector3[] GroundWallPosition;
 
               And in my otherScript
 public GameObject[] GroundWallObjects;
 
     void Start ()
     {
         GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
         foreach (GameObject Wall in GroundWallObjects)
         {
             GetComponent<LevelScript>().GroundWallPosition[Wall] = GroundWallObjects[Wall].transform.position;
         }
     }
 
               But obviously I cannot directly convert a gameObject into a Vector3, so how would I go about doing this?
Answer by jokim · Aug 19, 2014 at 06:36 PM
You're using a foreach loop as if it was an index based loop...
I don't believe you can use "Wall" as an index.
Try converting to a Index loop
     public GameObject[] GroundWallObjects;
     
     void Start ()
     {
         GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
         for (int i = 0; i < GroundWallObjects.Length; i++)
         {
             GetComponent<LevelScript>().GroundWallPosition[i] = GroundWallObjects[i].transform.position;
         }
     }
 
              Oh thank you very much, very clear and concise. However, I have run into a new problem, which is hopefully as easy to remedy. In another script I use the Vector3[] to compare my player position with the wall. $$anonymous$$y project is what you would consider a chess game to be, and I am comparing two positions, the before and after movements of the player. But if a player wants to move their piece off of the board, I cannot allow it, which is why I am trying to compare the positions of the player and the wall. I was sure this would work, but when I try to compare the two using:
 if ((transform.position + new Vector3 (0,-1,0)) != GetComponent<LevelScript>().GroundWallPosition)
 
                  It tells me that I cannot compare Vector3[] and Vector3. Is there an easier way to accomplish what I am trying to do?
Well, you'll need another loop here, to compare each and every one of the vector, if one match, then you turn a boolean to true.
Like so :
 bool wallHit = false;
 
 foreach (Vector3 wallPosition in GetComponent<LevelScript>().GroundWallPosition)
 {
     if (wallPosition == transform.position + new Vector3 (0, -1, 0))
     wallHit = true;
 }
 
 if (wallHit)
     //deny movement
 
                 Answer by AngryBurritoCoder · Aug 19, 2014 at 06:55 PM
This is pretty simple, one way you can do this...
 public LevelScript script; //drag script into here
 
 public GameObject[] GroundWallObjects;
 
 void Start()
 {
    GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
 
    for(int = i; i < GroundWallObjects.Length; i++)
    {
       script.GroundWallPostion[i] = GroundWallObjects[i].transform.position;
    }
 }
 
 
              Your answer