Trouble with level game object and script

Hi!

That’s strange but my level gameobject doesn’t have a serialised field for breakable blocks but I did serialize this integer in my script.

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

public class Level : MonoBehaviour
{

    // parameters
    [SerializeField] int breakableBlocks;  // Serialized for debugging purposes

    // cached reference
    SceneLoader sceneloader;

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

    public void CountBreakableBlocks()
    {
        breakableBlocks++;
        
    }

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

}

Make sure you saved the script and have no compiler errors, maybe try restarting Unity.

1 Like

Tried to reload unity. Didn’t help.
Also tried to delete level script as a component. Now I can’t add it as a component back to my level game object. There is an error.

I think I see the problem, you do have an error. Make sure to include the UnityEngine.SceneManager

1 Like

Sorry misread the code, that part should be fine…
Is there any errors in your console?

1 Like

Only these

You will have to fix those errors before the level script will compile properly, may I see your block script?

1 Like

Sure!
There it is

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

public class Block : MonoBehaviour
{

    [SerializeField] AudioClip breakSound;

    // cached reference
    Level level;

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

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

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

I can’t seem to find any problems, if you clear your console do the errors remain? Can you even play the game right now or does it say theres an issue?

1 Like

I can play, but there are some new warnings

Maybe I should remake a level prefab?

Check all your scripts and game objects and make sure they all have the correct referances

1 Like

I’m thinking the scripts are good, it’s something going on in the editor, are all the necessary game objects present in your scene and are they all hooked up correctly?

1 Like

I’ve checked all the code.
Pasted all the code from the GitHub. The only difference is I use vector3 in my paddle and ball scripts because the ball changes it’s Z position when the game starts. Everything else is just like in the course.

Only level1 scene lacks the level game object

I found the problem!

The name of the script file was not written with a capital letter. it was called “level” instead of “Level”. So the block script

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

couldn’t find the necessary script at all

Thank you very much for help, mate!

Right on! Glad you found the issue!

1 Like

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

Privacy & Terms