Hi all, I am having a bit of trouble getting individual objects in an array to change their material on collision. The problem I am having is that this happens across the array and not just to specific blocks. The first collision works correctly, but if i then collide with an un-hit block, its material skips to the secondHit material or thirdHit material (if 2 different blocks have been hit previously).
As you can see i am also changing mass on collision, this seems to be working fine, which just adds to my confusion.
How can i get the individual blocks in the array to hold their own hit value? I thought using a foreach loop would fix this.
any help would be brilliant, thanks in advance!
A.
public Material firstHit;
public Material secondHit;
public Material thirdHit;
public static int hitNumber = 0;
void OnCollisionEnter (Collision collision)
{
foreach (GameObject block in BlockCount.blocks) {
if (collision.gameObject == block) {
block.rigidbody.mass = block.rigidbody.mass / 2;
hitNumber++;
print(hitNumber);
if (hitNumber == 1) {
block.renderer.material = firstHit;
}
if (hitNumber == 2) {
block.renderer.material = secondHit;
}
if (hitNumber == 3) {
block.renderer.material = thirdHit;
hitNumber = 0;
}
}
}
}