- Home /
One of my variable won't increase. [Answered.]
I start learning unity recently and I am using Unity 2017.3.0f3. Now I'm creating a Block Breaker game and I tried to create a way to check win condition and to go to next level.
So I write those two variables:
public static int BreakableCount = 0;
public int RemindBricks = 0;
At start I write this code to count the number of blocks in my scene:
isBreakable = (this.tag == "Breakable");
if (isBreakable) {
BreakableCount++;
}
Which works perfectly and after that I set the win condition:
void OnCollisionEnter2D (Collision2D col)
{
Handlehits ();
print ("bricks count is= " + BreakableCount);
print ("remind brick is= " + RemindBricks);
print ("_______________");
if (RemindBricks == BreakableCount) {
SimulateWin();
}
}
void Handlehits ()
{
int MaxHit = hitsprites.Length + 1;
TimeHit++;
if (TimeHit >= MaxHit) {
RemindBricks++;
DestroyObject (gameObject);
} else {
LoadSprite();
}
}
My problem is RemindBricks only increased once and after that it won't increase at all. Here is the result:
And here my entire project to download if you want. So please tell me why my variable won't increase and how to fix it. Thanks.
Answer by Erowarrior · Jan 28, 2018 at 05:44 PM
Fixed it by making RemindBricks a static variable.
public static int RemindBricks = 0;
The reason this happened was because when an object got destroyed RemindBricks would go back to 0.