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!