Help with checking if gameobjects are the same on diagonal
Hallo, so I'm working on this game, and i got a little problem here. I'm trying to check if objects are the same on the diagonal. The objects im checking on are stored in a list for every row, Im using the llst and checking if the obejcts in the colums are having the same tag, but they are not getting removed and the above are not getting pushed down in the list.
Here are some of the code i use, I do this code in a lot of if else statements for all the cordinations and colors.
if (row1.Units[0].CompareTag("blue") && row2.selectedUnits[1].CompareTag("blue") && row3.selectedUnits[2].CompareTag("blue"))
{
GameObject t1 = row1.Units[0];
GameObject t2 = row2.Units[1];
GameObject t3 = row3.Units[2];
_GameManager.instance.PlayPoint();
row1.Units.Remove(t1);
row2.Units.Remove(t2);
row3.Units.Remove(t3);
Destroy(t1.gameObject);
Destroy(t2.gameObject);
Destroy(t3.gameObject);
row1.Cblue -= 1;
row1.tCount -= 1;
row2.Cblue -= 1;
row2.tCount -= 1;
row3.Cblue -= 1;
row3.tCount -= 1;
_GameManager.instance.Points += 30;
_GameManager.instance.needPoints -= 30;
_GameManager.instance.Klax += 1;
_GameManager.instance.needKlaxs -= 1;
if (row1.Units[0] == null)
{
for (int i = 0; i < row1.Units.Count - 1; i++)
{
if (row1.Units[i] != null)
{
row1.Units[i] = row1.Units[i + 1];
}
}
row1.Units[row1.Units.Count - 1] = null;
}
else if (row2.Units[1] == null)
{
for (int i = 0; i < row2.Units.Count - 1; i++)
{
if (row2.Units[i] != null)
{
row2.Units[i] = row2.Units[i + 1];
}
}
row2.Units[row2.Units.Count - 1] = null;
}
else if (row3.Units[2] == null)
{
for (int i = 0; i < row3.Units.Count - 1; i++)
{
if (row3.Units[i] != null)
{
row3.Units[i] = row3.Units[i + 1];
}
}
row3.Units[row3.Units.Count - 1] = null;
}
}
sometimes when I have an object getting added to one of the list it does what i want to, but it not consistently. And its like its never running the code even though i do it in update?
someone has any idéa whats could be wrong? (sorry for the english)
Answer by NoseKills · Dec 25, 2015 at 03:26 AM
This is the code for moving blocks down ?
if (row1.Units[0] == null)
{
for (int i = 0; i < row1.Units.Count - 1; i++)
{
if (row1.Units[i] != null)
{
row1.Units[i] = row1.Units[i + 1];
}
}
row1.Units[row1.Units.Count - 1] = null;
}
Looks wrong to me.
if bottom square of column 1 is null (should be) ->
start looping ->
first loop : i == 0
if (row1.Units[0] != null)
is false (bottom square should be empty/null) ->second loop : i == 1
if (row1.Units[1] != null)
is true (second square is not empty) ->row1.Units[1] = row1.Units[1 + 1];
you put the top most square into the middle square.
The "if" inside your loops only overwrites existing blocks with blocks above them.
if (row3.Units[i] != null) // if this is not an empty space
{
row3.Units[i] = row3.Units[i + 1]; // put the above block into it
}
You just need a loop that finds every empty space and puts the block above it into it something like
for (int i = 0; i < row1.Units.Count - 1; i++)
{
if (row1.Units[i] == null)
{
row1.Units[i] = row1.Units[i + 1];
}
}
row1.Units[row1.Units.Count - 1] = null;
There's no need for the outer "if" because i think you always want to move blocks down if there's an empty space below it... this rule applies also if you create some mechanic to remove other blocks than just the 3 mentioned in your example.
Thanks mate, this works sometimes, but its like my else if for the diagonal check is not working, i tried Debug.Log in the else if and its not firering when the conditions are right. Any idéa?
(in the upper first loop)