Compare GameObject with an array
I have been looking all over the place to see if i can get an answer to this, can i take a GameObject that i am looping already and compare that into another array?
I first find all the GameObjects in my game with the tag
var objects2 = GameObject.FindGameObjectsWithTag ("land_tile");
Then i see if they have a bool already attached to them and separate them into an array
foreach (var obj3 in objects2) {
if (obj3.GetComponent<ground_tile_script> ().upper_left_quadrant) {
GameObject[] temp_array_upper_left_quadrant;
Now here is where i have trouble, i want to compare the two. I want to loop through all the "land tiles" (obj4) and compare the attribute tile_row inside them to any of the GameObjects whom have the same attribute
foreach (var obj4 in objects2) {
if(obj4.GetComponent<ground_tile_script>().tile_row > **COMPARE TO AN ARRAY**.GetComponent<ground_tile_script>().tile_row) {
obj4.GetComponent<ground_tile_script>().lower_left_quadrant = true;
}
}
}
}
}
Although this isnt a solution to your problem, I would definitely recommend looking at C#'s Guide for na$$anonymous$$g conventions, since pretty much all of the official libraries and APIs for C# follow it. You can find this guide here: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/na$$anonymous$$g-guidelines
Answer by ransomink · Sep 15, 2017 at 03:25 AM
Yes, you can, but your setup is incorrect.
First of all, the naming convention makes it hard to follow.
Secondly, when you check for the variable upper_left_quadrant
, if successful, you're creating a new GameObject array every time instead of adding them into one array.
Lastly, I'm not sure what two you want to compare. The "land tiles" are one array but what is the other array you wish to compare against? Is it the one you created earlier? Because that is an array of objects with the upper_left_quadrant
variable, not the tile_row
. I believe you intend to:
Find all gameobjects with a specific tag
Check if each gameobject has a certain bool variable
Place those gameobjects inside of an array
Compare object variable against (whatever array are you trying to use) another object variable
private GameObject[] landTiles; private List<GameObject> upperLeftQuad; private void CompareAgainstArray() { if ( upperLeftQuad == null ) upperLeftQuad = new List<GameObject>(); else upperLeftQuad.Clear(); landTiles = GameObject.FindGameObjectsWithTag( "land_tile" ); foreach ( var tile in landTiles ) { if ( tile.GetComponent<ground_tile_script>().upper_left_quadrant ) { upperLeftQuad.Add( tile ); } } foreach ( var tile in landTiles ) { var row = tile.GetComponent<ground_tile_script>().tile_row; for ( int i = upperLeftQuad.Count; i > 0; i-- ) { if ( row > upperLeftQuad[ i ].GetComponent<ground_tile_script>().tile_row ) { tile.GetComponent<ground_tile_script>().lower_left_quadrant = true; } } } }
not sure if this is what you were trying to do...