It's just works, i don't know why

it’s my block scripts look like

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

public class Bloack : MonoBehaviour
{
[SerializeField] AudioClip Aesource;
Level level;
[SerializeField] GameObject blockSpark;
[SerializeField] int maxHits;
[SerializeField] int timesHit;
private void Start()
{
FirstSteap();
}

private void FirstSteap()
{
    level = FindObjectOfType<Level>();
    if (tag == "Yes")
    {
        level.CountBreakableBloacks();
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (tag == "Yes")
    {
        HandleHit();
    }
 }

private void HandleHit()
{
    timesHit++;
    if (timesHit >= maxHits)
    {
        DestroyBlock();
    }
}

private void DestroyBlock()
{
    ForAudio();
    Destroy(gameObject);
    level.BlockDestoryed();
    TriggerSparkVFX();
}

private void ForAudio()
{
    FindObjectOfType<Game_date>().addToScore();
    AudioSource.PlayClipAtPoint(Aesource, Camera.main.transform.position);
}

private void TriggerSparkVFX()
{
    GameObject sparkles = Instantiate(blockSpark, transform.position, transform.rotation);
    Destroy(sparkles, 2f);
}

}

but when i playing,if i destroy 1 block, all blocks will destroy in same time.
i want to check out what happened
so i did some change to Sceneloader’s scripts for stop load next scene with win

public void BlockDestoryed()
{
breakableBlocks–;
if(breakableBlocks <= 0)
{
// sceneloader.LoadNextScene();
}
}
then i go back to test,
blocks no longer destroy in same time
what is happening?

Hi,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hi @Nina
thanks for reply :slight_smile:
i have already compared both script (my code & rick’s code)
both script have same code (Only the function names are different)

i think maybe i was changed block’s script and forgot to save.

BTW,i have no idea what code should i using in Debug.log like this error :stuck_out_tongue:

From what I understood, you already identified the problem:

If all blocks get destroyed at the same time, add Debug.Logs to the method that destroys a block to figure out if all methods get called. This way, you can figure out if the issue is caused by something that calls these methods.

If the value of breakableBlocks determines whether the next scene gets loaded and you suspect that the issue might be here, log the value into your console.

Never assume that your code is working as expected. A lot is going on during runtime.

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

Privacy & Terms