I’m recreating an old crash bash pogo game; I have 64 tiles in an array and they each have different materials attached to them allowing them to change to a players colour when collided with but now I’ve got a points banking box that needs to loop through all 64 tiles to find which tiles have a specific colour material attached so it can allocate the points to that player or enemy.
I’ve been trying a foreach loop to find and name each tile with a players material colour but using that one is causing all 64 tiles to turn that players colour rather than just name the ones that are currently the players colour. Should I be using a different loop or introduce a break clause somehow? Any help is appreciated.
Here is my code so far…
public class Points_Banker : MonoBehaviour
{
public Material TileOriginal;
public Material Player1Mat;
public Material Enemy1Mat;
public GameObject[] tiles;
public GameObject player = null;
public GameObject enemy1 = null;
private void OnTriggerEnter(Collider other)
{
if (other.name == "Player 1")
{
AddToPlayerScore();
}
else if (other.name == "Enemy 1")
{
AddToEnemy1Score();
}
}
public void AddToPlayerScore()
{
player.GetComponent<PlayerMoveTest>().itemCollection.Play();
foreach (var item in tiles)
{
if (item.GetComponent<Renderer>().material = Player1Mat)
{
Debug.Log(item.name);
player.GetComponent<PlayerMoveTest>().score += 1;
Destroy(gameObject);
}
}
}
public void AddToEnemy1Score()
{
enemy1.GetComponent<EnemyTest>().itemCollection.Play();
foreach (var item in tiles)
{
if (item.GetComponent<Renderer>().material = Enemy1Mat)
{
Debug.Log(item.name);
enemy1.GetComponent<EnemyTest>().score += 1;
Destroy(gameObject);
}
}
}
}