Cannot Get Zero to Block Numbers(With Codes)

I tried to use exact lesson codes and
gameobject.findGameObjectsWithTag(“breakable”).lenght
but cannot get zero value to integer.

HERE IS THE CODES, THEY’RE ALMOST SAME WITH COURSE
Codes

Hello!

I just had a look in your code and just noticed that in the Start function you have

if (isBreakable) {
	BlockNumber ;
}

You forgot to increment BlockNumber, this is why it stays to 0

if (isBreakable) {
    BlockNumber++ ;
}

I hope this solves your problem.

Cheers! :slight_smile:

Actually I do not know why but there’s something wrong about code so I changed the link. I have issue with getting zero I can count them and they subtracting till “1” but zero. Thanks for your effort but I couldn’t pass this lesson for a week now :smiley:

Can you pop a screenshot up of your scene view and also one of the hierarchy with your brick/rows of bricks all expanded.

Thanks

There’re two bricks in level 1 to experiment my coding but my other levels are better as visual but this is not issue I guess :smiley: . I will be grateful if you can solve this, thanks to your attention anyway

1 Like

Thanks for the screenshot, for testing purposes, just having one or two bricks will be better anyway.

So, to be clear, are you saying that, if you output the number of breakable blocks remaining in the scene, after you destroy both of these, the value remains at 1 and never reaches zero?

yes, exactly. Actually in the beginning I used findgameobjectwithtag code (I think it’s more easy to understand) but after that I experiment this problem and I used this code (code you see now) . In every level I can count them, subtract them but cannot get zero.
I downloaded section end project and saw that it’s working but it always adds amount of bricks when you lose and restart game (you can print it). There are two problem but about same thing.

1 Like

Can you paste your Brick.cs code in to your reply here please.

Regarding the second problem, make sure you set the breakable brick count to 0 before you load any scene. Because the variable is static, its value is persisted through out the game.


LevelManager.cs

using UnityEngine;
using UnityEngine.SceneManagement;

public class LevelManager : MonoBehaviour
{
    /// <summary>
    /// Loads the level with the specified name
    /// </summary>
    /// <param name="name">The name of the level to load</param>
    public void LoadLevel(string name)
    {
        Brick.breakableCount = 0;

        Debug.Log("Level load requested for : " + name);
        SceneManager.LoadScene(name);
    }

    /// <summary>
    /// Loads the next level in the scene build index order
    /// </summary>
    public void LoadNextLevel()
    {
        Brick.breakableCount = 0;

        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
    }

    /// <summary>
    /// Determines whether the next level should be loaded
    /// </summary>
    public void BrickDestroyed()
    {
        if (Brick.breakableCount <= 0)
        {
            LoadNextLevel();
        }
    }

    /// <summary>
    /// Quits the game
    /// </summary>
    /// <remarks>Limited functionality on Web and Debug/Editor builds, bad practice for mobile devices</remarks>
    public void QuitRequest()
    {
        Debug.Log("I want to quit!");
        Application.Quit();
    }
}

See also;

Code exists in the top message, here again:
Codes

Just wanted to check nothing had changed since you made your post, it’s also a lot easier for people to help you if you include the code in your forum post rather than else where, the formatting is better etc.

One thing that stands out to me is that you are reducing the block count after destroying the brick, as a quick test, can you swap that around;

void TackleCollision ()
{
    if (timesHit >= maxHits) 
    {
        Destroy (gameObject);   // swap this line and the following line around
        BlockNumber--;
    } 
    else
    {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[timesHit-1];
    }
}

If that still doesn’t make any difference, can you zip up your project files and upload it so that I can have a quick look, in context. The forum will support a file size of 10mb, anything over that and you would need to use Google Drive / Dropbox etc and provide a link to the download.

1 Like

Project Files (Dropbox)

Here the file I had tried that approach and did it again for you but nothing changed. Thank you again I hope you can find problem

Thanks - just opening the project now.

Just one thing, before I get into it - do you happen to have the BrickShot script component on the Bricks GameObject which is the parent two the two actual bricks?


Updated Thu Nov 09 2017 15:28

Ok, forget the above, I’ve had a look myself.

The issue isn’t actually an issue, you just have your print statement which outputs the number of remaining blocks in a less than ideal location.

If you think about it, there are only two places when the block count is going to change, when the level loads and all of the blocks are initialised, and when one gets destroyed.

I moved your print statement to the TackleCollision method, and the output is correct. After the first block is destroyed it states there is 1 block remaining, when the second block is destroyed it states that there are 0 blocks remaining;

void TackleCollision ()
{
    if (timesHit >= maxHits) 
    {
        BlockNumber--;
        print ("Remaining Blocks " + BlockNumber);	
        Destroy(gameObject);
    } 
    else
    {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[timesHit-1];
    }
}

image

Your next issue is that the scene doesn’t change when you get to zero blocks remaining, if we take a look at the code that should handle that it’s here, in the LevelManager script;

public void brickDestroyed ()
{
    if (BrickShot.BlockNumber == 1) 
    {
        LoadNextLevel();
    }
}

Looking at the above, you may be wondering why it is then that the scene doesn’t change after the first block is destroyed, and that’s because you have removed the call to this method from the Brick script (BrickShot.cs)

void TackleCollision ()
{
    if (timesHit >= maxHits) 
    {
        BlockNumber--;
        print ("Remaining Blocks " + BlockNumber);	
        Destroy(gameObject);
    } 
    else
    {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[timesHit-1];
    }
}

should be

void TackleCollision ()
{
    if (timesHit >= maxHits) 
    {
        BlockNumber--;
        levelManager.brickDestroyed();    // you are missing this line
        // print ("Remaining Blocks " + BlockNumber);	
        Destroy(gameObject);
    } 
    else
    {
        this.GetComponent<SpriteRenderer>().sprite = hitSprites[timesHit-1];
    }
}

finally, we should change the BrickDestroyed (brickDestroyed) method to load the next scene when the bricks remaining = 0;

public void brickDestroyed()
{
    if (BrickShot.BlockNumber <= 0)
    {
        LoadNextLevel();
    }
}

On a side note, you have a lot of inconsistencies with naming going on in your scripts, I would highly recommend taking a consistent approach as it will help you in the long term.

For example, method names starting with a capital letter;

public void LoadNextLevel()
{
}

as opposed to;

public void loadNextLevel()
{
}

Variable names like this;

LevelManager levelManager;

as opposed to this;

LevelManager levelmanager;

camelCase / PascalCase

Keeping your code tidy will really help you, so remove any surplus white spare that isn’t helping, space your code out neatly so it’s easier to read, keep indentation consistent, at the very least it will help others when you paste your code for others to read. :slight_smile:

Also, you have two LevelManagers on the scenes, one GameObject called LevelManager and then a prefab instance called Scripter which has a LevelManager script attached. This kind of duplication will invariably lead to complications, I recommend removing one.

I can confim after making the above changes the bricks get destroyed and the second scene loads as expected.

Hope this helps :slight_smile:

1 Like

I am sorry but I couldn’t understand it. Bricks gameobject is parent of the “1shot” named bricks that’s all I can say about what I get

1 Like

That’s ok, I checked myself before updating my post above.

The reason I asked is that I have seen students collapse the parent GameObject, and then select it to apply change to everything inside, if you were to attach a Brick script to the parent object it would effectively think you have another brick, worse still, if you tagged it as breakable, you’d have a block that couldn’t ever be destroyed if it didn’t also have a collider! This would also give you the additional blocks in the scene.

See my previous post for a resolution to your issue. :slight_smile:

I really thank you. I’m trying everything in a way different (as you can see :slight_smile: ) and I’m messy. I will try your solution now. GoodBye

1 Like

Trying stuff differently is a great way to learn, my suggestions above regarding consistency are just there to help you - it’s always easier to debug :slight_smile:

Let me know how you get on, any problems just shout :slight_smile:

1 Like

Privacy & Terms