I'm stuck with tags and a host of other issues

Kind of stuck with tags Lost the SFX Sparkles Effect and the Counter isn’t working anymore, and for some reason the left side of my game’s unbreakable blocks are just passed through by the ball here is my code and I can’t find whats wrong.

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

public class Block : MonoBehaviour
{
    [SerializeField] AudioClip breakSound; //Create audio source 
    [SerializeField] GameObject blockSparklesVFX; // searlizing for the sparkle effect

    //Cache Reference
    Level level;

    private void Start() //sets it to tag breakable identity ** better to tell it how to destroy then not to destroy cause indestructible is defualt** // Calls the Count the blocks by adding 1 of Level Script // Method to add up all blocks
    {
        CountBreakableBlocks();
    }

    private void CountBreakableBlocks()
    {
        level = FindObjectOfType<Level>();
        if (tag == "Breakable")
        {
            level.CountBlocks();
        }
    }

    //example
    //[SerializeField] Level level; Ex of Linking up way to count blockable breaks gives good control

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (tag == "Breakable")
        {
            Destroy(gameObject);
        }

    }

    private void DestroyBlock()
    {
        PlayBlockDestroySFX(); //(Create audio souce) play clip at point its breakSound and its going be the main camera transform position // //Calls Gamestatus Script and uses the AddToScoreFunction with the DetroyBlock funct that already has a counter from levels script to help set score
        Destroy(gameObject); //small g game object means the specific game object that the script is in
        level.BlockDestroyed(); //Calls The BlockDestroyed Function in Level Script that is Cached //load next scene??
        TriggerSparklesVFX(); // Calls the SparklesVFX funct

    }

    private void PlayBlockDestroySFX()
    {
        FindObjectOfType<GameSession>().AddToScore();
        AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);
    }

    private void TriggerSparklesVFX()
    {
        GameObject sparkles = Instantiate(blockSparklesVFX, transform.position, transform.rotation); // simplest bit of instantiate is just instantiate(object original) we need poistion and rotation
        Destroy(sparkles, 1f); // need to make sure its not on going so must destroy the effect
    }

}

You need to get the tag of each specific gameObject that you’re trying to query. For example:
if (collision.gameObject.tag == “Breakable”)
Same with your CountBreakableBlocks() method, you need to iterate over the blocks and check the tag of each to determine if it’s breakable

Philip,

I realize that you have probably moved on by now, but I am making my way through this course and noticed your post.

Lost the SFX Sparkles Effect and the Counter isn’t working anymore

Looking over your code the problem appears to be in your OnCollisionEnter2D method. You have “Destroy(gameObject)” where what you probably intended was “DestroyBlock()”

the left side of my game’s unbreakable blocks are just passed through by the ball

Not sure what would be causing this, but just for sanity, maybe double check that these objects haven’t had their colliders removed or disabled. Also, consider reverting the failing objects to the prefab and see what happens.

1 Like

im having a problem with my unbreakable blocks. they are there and they dont break they do exactly what they’re supposed to do. but they’re invisible in play screen. i can see them in the scene window but not in the game window

Privacy & Terms