I can't go to Next Level

well i am new this community and new to this language as well but I am working super hard to keep up but in the block breaker I can’t seem to go to next level even after i have cleared all the blocks

here is my code please help me to find the error

Hi Pavan,

On line 28 of Level.cs you are checking whether the number of breakable block is greater than or equal to 0. If you are reducing the count of breakable blocks, you will want that to be less than, e.g.

public void BlockDestroyed()
{
    breakableBlocks--;

    if(breakableBlocks <= 0)
    {
        sceneLoader.LoadNextScene();
    }
}

It’s hard to tell whether this is your only issue, as potentially, whenever you destroyed one block at the moment, assuming to had more than one block in the scene, your game should have loaded the next scene straight away. The reverse of what you want, but a load of scene none-the-less.

If this isn’t happening I would be inclined to check the GameObjects you have in your scene, specifically Level and SceneLoader and ensure that these have the corresponding script components that they will need in order to function. In addition, make sure you have all of your scenes added to your Build Settings.

I hope the above helps and if this doesn’t resolve the issue please let me know. :slight_smile:

Also, it is much better to copy/paste your code into your posts on the forum rather than using screenshots. Screenshots tend to not be as readable, especially on mobile devices which then require zooming/scrolling. Also, you prevent those that offer to help you from copy/pasting a chunk of your code back to you with a suggestion/correction meaning that they will have to type it all out. You will often find it’s easier to gets responses to your queries if you make it easy for people to reply. Please review the link below regarding applying code formatting to your copy/pasted code for future reference.


See also;

the code is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Level : MonoBehaviour
 {

    // config parameters
    [SerializeField] int breakableBlocks; // seralized to find out how many blocks remain

    //cached reference
    SceneLoader sceneLoader;

    private void Start()
    {
        sceneLoader = FindObjectOfType<SceneLoader>();
    }

    public void CountBreakableBlocks()
    {
        breakableBlocks++;
    }
    public void BlockDestroyed()
    {
        breakableBlocks--;
        if(breakableBlocks <= 0 )
        {
            sceneLoader.LoadNextScene();
        }
   


}

here is my level .cs code

and my block .cs code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;

public class Block : MonoBehaviour
{
    [SerializeField] AudioClip breakSound;

    Level level;
    private void Start()
    {
        level = FindObjectOfType<Level>();
        level.CountBreakableBlocks();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Blockable();
    }

    private void Blockable()
    {
        AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
        Destroy(gameObject);
        level.BlockDestroyed();
    }
}

Thank you Rob but even though I changed the code in Line 28 i still can’t load to next Scene
I checked and rechecked my build settings to and also
and there is also a problem.
It seems that Even after destroying the blocks the number of Breakable blocks is
not going down

Hi Pavan,

You asked me about the characters to use for the code formatting, I’m not entirely certain what it’s called, but for me its on the tilde key, top left next the number 1. Depending on your keyboard/language and so on this may not be the case. Probably the easiest thing to do is edit your post above, you’ll then be able to see the characters that I have replaced your apostrophes with.

It would probably be easier to diagnose your project with a copy of it, but let’s see if we can work through a few things first.

  • Do you have any errors in the console?

  • Your method Blockable within Block.cs makes a call to level.BlockDestroyed. In order for this to work you would need a GameObject in your scene with the Level.cs script attached to it, do you have this?

  • In your Level.cs script you have a serialized field breakableBlocks, when you run your game, before you launch the ball, if you select the Level GameObject you should see a count of how many breakable blocks you have in the scene - does this value look to be correct?

  • In the BlockDestroyed method, let’s place a Debug.Log statement to indicate that the method is being called when a block is destroyed, update you code as follows;

    public void BlockDestroyed()
    {
        Debug.Log("BlockDestroyed method has been called.");
    
        breakableBlocks--;
        if(breakableBlocks <= 0 )
        {
            sceneLoader.LoadNextScene();
        }
    }
    

    With the above change, when you run the game and break a block, do you see the message output to the console?

  • assuming all is good so far, check to see if you have accidentally added the Block.cs script to a GameObject in the scene which cannot be destroyed, for example, some people often accidentally attached scripts as components when they drag/drop them. If you dropped it onto the Main Camera, for example, you’re block count would go up but you’d not be able to reduce it because you can’t destroy the GameObject it’s added to. Work your way through the Hierarchy and look at each GameObject and child GameObject where appropriate and see if you have the Block.cs attached to anything it shouldn’t be.

  • finally, run the game, just before you destroy the last block, pause the game, select the Level GameObject and view its details in the Inspector. Does it state that there is one remaining block, or more?

Hello Rob,
Thank you I have figured out to use the code function in this community .
Thank you for making my problems into simple straight forward points it gave me the insight of the problem and gave me an eagle view of the whole problem and where to look for it.

The problem I have got is that the BreakableBlocks [seralizefield] is not going to 0 even after I destroy the blocks

Here I started with with 2 Blocks to make sure my script is working but even after I destroyed one game object the BreakableBlocks in Level script is still remaining as 2 .

Please help me to resolve that problem

Did you try all of the items I mentioned above?

I have got the following error and warning
IndexOut of rangeException

And I have got a warning about inconsistency

Now let me make a list of scripts that I have assigned so we could see that if I have made an error some where

Main-camera or rather Game camera has an audio listener
Game Canvas has it’s own components
sceneLoader has it’s own script
Level has scene loader as well as Level script in it.
Play space has it’s back ground and other colliders such as left and right walls
Paddle and Ball has it’s own script and sound

I have created a game object for blocks and in it the blocks are assigned with Block script

This as far as I know I would really appreciate the help if you can find the bug

  • you don’t need the SceneLoader.cs script as a component of the Level GameObject.

  • I would resolve your line endings issue if I were you, whilst it might not be, the secondary error could be related if something has failed to load.

  • you haven’t stated whether you added the Debug.Log statement and if so, did you see the output from it?

  • Can you confirm whether the Blocks GameObject has any scripts attached to it also please.

Well thank you the error has been resolved and
I can jump into the next level now
The error seems that i didn’t update my last level prefab so the script was in there so there was the error

Thanks to you for helping me out

I now know whom I have to text if i have any problems

1 Like

I’m glad to hear you’ve resolved the issue Pavan and come move forward with the course again :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms