0
\$\begingroup\$

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:

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.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I'm not sure if this is what you were going for but this:

My problem is RemindBricks only increased once and after that it won't increase at all. Here is the result:

Is solved by making the RemindBricks variable static.

After you are increasing the value of RemindBricks, you're deleting the game object and (I think) since your RemindBricks is not a static variable, it'll get removed along with the object and will be set to 0 again. Static variables won't be affected.

So in stead of public int RemindBricks = 0; try public static int RemindBricks = 0;

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .