• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jamal_Alkelani · Aug 11, 2017 at 08:07 AM · unity 5updatevariablesnew user

Variable is not updated

Hello everyone, I'm new to unity and I've faced this problem I hope you help me with it.

I'm making a block breaker game like arkanoid game and I'm trying to load the next level when I hit the destroy all the bricks in the game, so I made a static integer variable called (blocksCount) that counts the number of the blocks in the game and each time I destroy a block I decrements the (blockCount) by 1 until it becomes 0 so I load the Win screen.

this is my code :-

         using System.Collections;
         using System.Collections.Generic;
         using UnityEngine;
 
         public class HitCounter : MonoBehaviour {
     
     private int hitTimes;
     private LevelManager levelmanager;
     public Sprite[] sprites;
     private static int bricksCount=0;
     // Use this for initialization
     void Start () {
         hitTimes = 0;
         levelmanager = GameObject.FindObjectOfType<LevelManager> ();
         if(this.tag=="Breakable")
         bricksCount++;
     }
     
     // Update is called once per frame
     void Update () {
         if (bricksCount <= 0) {
             levelmanager.loadNextLevel ();
         } else
             print ("brlocks remaining are " + bricksCount);
 
 
     }
     void OnCollisionEnter2D (Collision2D collision)
     {
         if (this.tag == "Breakable")
             executeAction ();
 }
 
     void executeAction()
     {
         int maxHits = sprites.Length+1;
         hitTimes++; //increasing the total number of block hits
         //TODO ask why we destroy a game object and how does we know that the game object is the specified block
         if (hitTimes >= maxHits) {
             bricksCount--;
             DestroyObject (gameObject);
 
         }
         else {
             if(sprites [hitTimes - 1]!=null)
             this.GetComponent<SpriteRenderer> ().sprite = sprites [hitTimes - 1];
         }
     }
     }
 

The problem is that when I destroy the last block in the game it doesn't load the next level I tried print a message shows how many bricks are still inside the update method, but unfortunatly it says there stills another 1 block but actually all blocks are destroyed.

I've fixed that by moving the code from the update method into the ((OnCollisionEnter2D)) method and that worked perffectly.

as follows:-

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HitCounter : MonoBehaviour {
     
     private int hitTimes;
     private LevelManager levelmanager;
     public Sprite[] sprites;
     private static int bricksCount=0;
     // Use this for initialization
     void Start () {
         hitTimes = 0;
         levelmanager = GameObject.FindObjectOfType<LevelManager> ();
         if(this.tag=="Breakable")
         bricksCount++;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     void OnCollisionEnter2D (Collision2D collision)
     {
         if (this.tag == "Breakable")
             executeAction ();
 }
 
     void executeAction()
     {
         int maxHits = sprites.Length+1;
         hitTimes++; //increasing the total number of block hits
         //TODO ask why we destroy a game object and how does we know that the game object is the specified block
         if (hitTimes >= maxHits) {
             bricksCount--;
             DestroyObject (gameObject);
             if (bricksCount <= 0) {
                 levelmanager.loadNextLevel ();
             } else
                 print ("brlocks remaining are " + bricksCount);
 
 
         }
         else {
             if(sprites [hitTimes - 1]!=null)
             this.GetComponent<SpriteRenderer> ().sprite = sprites [hitTimes - 1];
         }
     }
     }
 
 
 

Could someone tell me why that happened ?? where the previous code is right and thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sadowlight123 · Aug 11, 2017 at 09:57 AM

Hello , I think that removing this line of code might solve it.

 bricksCount++;

in your start function you are adding one extra brick to your total brick count

 void Start () {
          hitTimes = 0;
          levelmanager = GameObject.FindObjectOfType<LevelManager> ();
          if(this.tag=="Breakable")
          bricksCount++;
      }

Please let me know if it solved the problem

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamal_Alkelani · Aug 11, 2017 at 10:03 AM 0
Share

At the start function I increment the brciksCount so that I get the total number of bricks in the scene, that would help me to detect if the the user destroyed all the bricks so that I load to the next level, so it's right 100%

avatar image sadowlight123 Jamal_Alkelani · Aug 11, 2017 at 10:23 AM 0
Share

have you tried checking that the count initially is correct ? use :

 Debug.Log(bricksCount);

$$anonymous$$indly try :

 void Start () {
           hitTimes = 0;
           levelmanager = GameObject.FindObjectOfType<Level$$anonymous$$anager> ();
           if(this.tag=="Breakable")
           bricksCount++;
          Debug.Log(bricksCount);
       }

Please let me know what happens

avatar image Jamal_Alkelani · Aug 11, 2017 at 10:41 AM 0
Share

Yes, I've tried that it gives me right result, and every time I destroy a brick it decrements by 1 until the LAST brick it stills 1 not 0

avatar image
0

Answer by brazmogu · Aug 11, 2017 at 10:13 AM

I think the problem here is simply of order of execution. You're updating bricksCount inside the collision code, and in there you destroy the object right away. So what probably was going on is that when the last Update for this object was run, you still had 1 block, and when you would have 0 blocks, the object doesn't exist anymore.

Your solution is exactly what I'd suggest to do(change level right when the counter is decremented), but you could also set this logic in another object that checks for HitCounter.bricksCount, if it were a public variable instead(or there was some way to access it from outside).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamal_Alkelani · Aug 11, 2017 at 10:21 AM 0
Share

Yes I also thought that, but I decrements the bricksCount before I destroy the brick ! and I can see that the brick is destroyed so the variable must be decremented. $$anonymous$$y problem is not with loading the level the problem is the variable isn't decremented when the LAST brick is destroyed .

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNET - UI Not updating with Healthbar and other bars. 1 Answer

UNet Weaver Error, on project updation 1 Answer

2017.2 update from 2017.1 1 Answer

advertisement existed in both editor 1 Answer

Rotating object breaks limitations/Bounds 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges