How do I Access Variables of Object Within an Array of GameObjects?

I’m working in C# to start with. I’m trying to cut out unnecessary code to keep this short, so here is roughly what I have.

public int boardXCoord;
public int boardYCoord;
public GameObject[] segments = new GameObject[88];
public int[, ] gGrid = new int[8, 11];

void Update ()
{
     for(int segAI = 0; segAI < 88; segAI++)
          {
               if(GameObject segment = new GameObject(segments[segAI]) != null)
                    {
                         GameObject segment = new GameObject(segments[segAI]);
                         int occupiedXCoord = segment.boardXCoord;
                         int occupiedYCoord = segment.boardYCoord;
                         gGrid[occupiedYCoord, occupiedXCoord] = 1;
                    }
          }
}

The object that is running this code has a boardXCoord and boardYCoord variable, and so do all of the other objects that will be in the segments array. As this game goes on, I will add more GameObjects to the Segments array. I then want to get the coordinates of each existing GameObject in the Segments array and set the value of the corresponding location within my gGrid two dimensional array to one so that I know that it is already occupied. I just can’t figure out how to access the variables of each object in the segments array. I’m sorry about how poor this code is. I only get a little bit of time to practice coding, and am very new. Thanks, everyone.

--------------------------------EDIT--------------------------------

For anyone having this same problem, I did find a work around. It doesn’t work exactly as I was hoping to have my code work, but it gets the job done.
Tag all objects you want to get variables from with whatever tag fits your situation.

Create an array of GameObjects — GameObject objectsWithTag;

Assign objects with tag to the array — objectsWithTag = GameObject.FindGameObjectsWithTag(“YourTagHere”);

Then itterate through the array searching for whatever — foreach(GameObject objectWithTag in objectsWithTag){Do Something}

Accessing whatever you’re after is easy as creating a variable, then just pulling the variable from the script attached to your object. — int aNumber = objectWithPlayerTag.GetComponent().thisIsTheVariableYou’reLookingFor;

Hope I’ve helped someone!

If I’m reading your question and code correctly - and bear in mind it’s 5am here and it’s been a busy night shift so far - your array of segments is populated with segment game objects?

If that is the case, then you can access each segment’s boardXCoord and boardYCoord simply by referring directly to each segment in the array - i.e.:

segments[segAI].boardXCoord = 0;
YPos = segments[segAI].boardYCoord;

Above is an example of writing and reading to an individual segment in the array.

Hope this helps - can’t test the code as I’m at work!

I’m confused by your code, and there is some context missing for me to give you an accurate answer, but let me give you a ideas. First let me assume you want to access the position (stored the transform) of one of your game objects in the array:

Vector3 pos = segments[segAI].transform.position;

But you don’t want to use pos.x and pos.y directly to index into your qGrid array. The problem is that pos.x and pos.y are floating point numbers, with floating point imprecision. So what you think is 3.0, might be stored as 2.99999999999999999. And when converted to an int, it will truncated to 2.0. Instead you can do it this way:

  gGrid[Mathf.RoundToInt(pos.x), Mathf.RoundToInt(pos.y)] = 1;

Note it might be better rather than use the object position for indexing into an array, to instead store the row/col in a component on the game object. Or even to have a 2D array of game objects that map directly to the rows/cols of the board. If aGrid was an array of game objects, then a non-null value would indicate the space was occupied.

Looking at your code, I don’t understand what you are trying to do with lines 10 and 12. It appears that you are trying to pass a game object as a parameter to the GameObject constructor. And I’m not sure of the purpose. In fact I’m surprised this code even compiles given I cannot find a constructor for a GameObject that takes a GameObject as a parameter.