In the code posted below I am trying to get tags from the 3 indexes in “tileSpawnPoints”
and then compare them to each other to get the “debug.log(“Win!”);” but it won’t work. It
looks like its giving me the wrong object linked to the index for example “tileSpawnPoints[0] returns the third object in the array instead”
if (tileSpawnPoints[0].GetComponent<TileTapped>().isTileTapped == true && tileSpawnPoints[1].GetComponent<TileTapped>().isTileTapped == true && tileSpawnPoints[2].GetComponent<TileTapped>().isTileTapped == true)
{
if (tileSpawnPoints[0].transform.GetChild(tile) != null && tileSpawnPoints[1].transform.GetChild(tile - 1) != null && tileSpawnPoints[2].transform.GetChild(tile - 1) != null && tileSpawnPoints[0].transform.GetChild(tile - 1).tag == tileSpawnPoints[1].transform.GetChild(tile - 1).tag && tileSpawnPoints[0].transform.GetChild(animal).tag == tileSpawnPoints[1].transform.GetChild(animal).tag)
{
if (tileSpawnPoints[1].transform.GetChild(tile) != null && tileSpawnPoints[2].transform.GetChild(tile - 1) != null && tileSpawnPoints[0].transform.GetChild(tile - 1) != null && tileSpawnPoints[1].transform.GetChild(tile - 1).tag == tileSpawnPoints[2].transform.GetChild(tile - 1).tag && tileSpawnPoints[1].transform.GetChild(animal).tag == tileSpawnPoints[2].transform.GetChild(animal).tag)
{
Debug.Log("Win!");
}
}
}
I suggest splitting those checks up into there seperate parts and then running your checks like so…
var animal;
var tile;
public void TileCheck()
{
if(TileIsTapped(tileSpawnPoints[0])
&& TileIsTapped(tileSpawnPoints[1])
&& TileIsTapped(tileSpawnPoints[2]))
{
if(ChildTileExists(tileSpawnPoints[0], 0)
&& ChildTileExists(tileSpawnPoints[1], -1)
&& ChildTileExists(tileSpawnPoints[2], -1)
&& TileTagsMatch(tileSpawnPoints[0], -1, tileSpawnPoints[1], -1)
&& AnimalTagsMatch(tileSpawnPoints[0], tileSpawnPoints[1])
)
{
if(ChildTileExists(tileSpawnPoints[1], 0)
&& ChildTileExists(tileSpawnPoints[2], -1) //Alrady checked in previous if statement
&& ChildTileExists(tileSpawnPoints[0], -1)
&& TileTagsMatch(tileSpawnPoints[1], -1, tileSpawnPoints[2], -1)
&& AnimalTagsMatch(tileSpawnPoints[1], tileSpawnPoints[2])
)
{
Debug.Log("Win!");
}
}
}
}
//Remove the offsets if they stay constant at - 1 and hard code it
public bool TileTagsMatch(GameObject parent_1, int p1_offset, GameObject parent_2, int p2_offset)
{
if(parent_1.transform.GetChild(tile + p1_offset).tag == parent_2.transform.GetChild(tile + p2_offset).tag)
{
return true;
}
return false;
}
public bool AnimalTagsMatch(GameObject parent_1, GameObject parent_2)
{
if(tileSpawnPoints[0].transform.GetChild(animal).tag == tileSpawnPoints[1].transform.GetChild(animal).tag)
{
return true;
}
return false;
}
public bool ChildTileExists(GameObject parent, int offset)
{
if(parent.transform.GetChild(tile + offset) != null)
{
return true;
}
return false;
}
public bool TileIsTapped(GameObject tile){
{
if(tile.GetComponent().GetComponent<TileTapped>().isTileTapped)
{
return true;
}
return false;
}
After rewriting your code to be an example, I can’t help but think that what ever you are doing this is a bad way to do it.
I suggest thinking about what it is that you want to acheive and rethink how to do it.