Personal challenge: brick colour based on # of hits remaining

Hi guys,

I decided to jump ahead at this stage and have bricks colour themselves dynamically based on their remaining number of hits. So in my head I planned how I thought this might work and I just wanted to know if I’m approaching it in the best possible way:

  • Add several vec3 (vec4?) brickColour public variables, configure each in prefab
  • Add if conditions to dynamically change SpriteRenderer.color based on a new variable ‘int remainingHits’

Already I’ve run into some problems… I thought it would be enough to create several public vec3 but already that’s displaying an error:

Next up, I would have thought it would suffice to access this.SpriteRenderer.color and set that as the appropriate colour in each case, but that is showing up in red…

I realise it’s probably just a case of me not using the proper syntax, but I would’ve thought this in particular would be pretty straightforward…

It’s Vector3 or Vector4. Probably Vector4 as aside from R,G, B you also have A for alpha colour. I’m not sure if how I approached it is the right way but I had some issues doing it the way you are doing it.

I did the following. I created a public Color variable for instance hitColour and then set its colour in the inspector. Then I assigned it like you are planning to do like

at the top as variables:


public SpriteRenderer spriteColour; 


public Color hitColour; // this colour is set in the editors inspector



void BrickHit()
{
spriteColour.color = hitColour;
}

That’s how I got it to work although again I’m not sure if that’s the correct or easiest way.

Ah, so Vector3 and not vec3 hehe. I have a background as an FX Artist and have done shader programming and vec3 is what I use in that context. I just stubbornly expected it to work in this case too :slight_smile:

Hi my name’s Thiago, well I’ve don’t tried to do this color change with a Vector3, but I’m using the Switch Statement to make my bricks change and it’s working for now.

The Statement that I’ve used fit in the code between the count of TimesHit++ and If Statement

See the code:

void OnCollisionExit2D (Collision2D collision){
TimesHit++;
switch (TimesHit) {
case 1:
this.GetComponent ().sprite = hit1;
break;
case 2:
this.GetComponent ().sprite = hit2;
break;
case 3:
this.GetComponent ().sprite = hit3;
break;
default:
this.GetComponent ().sprite = hit0;
break;
}
if (TimesHit >= maxHits) {
Destroy (gameObject);
}
}

P.S.: Sorry for my bad English I’m Brazilian.

Hope it helps.