Cant assign clip to break object like what you did in the lecture 63

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Block : MonoBehaviour

{

[SerializeField] AudioClip breaksound;

private void OnCollisionEnter2D(Collision2D collision)

{

    AudioSource.PlayClipAtPoint(breaksound, Camera.main.transform.position);

    Destroy(gameObject);

}

}

every time the ball hit the block its tell me a problem and the block didn’t destroy

{

NullReferenceException: Object reference not set to an instance of an object

Block.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/Block.cs:9)

}

when i remove this line

AudioSource.PlayClipAtPoint(breaksound, Camera.main.transform.position);

its working fine again

Hi Mohammed,

Could you format your code to increase its readability, please?


See also:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour
{
  

    //config param.
    [SerializeField] GameObject blockSparklesVFX;
    [SerializeField] AudioClip breakSound;
    //Cached reference
     Level level;
    //state variable
    [SerializeField] Sprite[] hitSprites;
    [SerializeField] int timesHit;
    
    private void Start()
    {

        CountBreackableBlocks();
    }

    private void CountBreackableBlocks()
    {
        level = FindObjectOfType<Level>();
        if (tag == "Breackable")
        {
            level.CountsBlocks();
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        

        if (tag == "Breackable")
        {
            HandleHit();
        }
    }


    private void HandleHit()
    {
        timesHit++;
        int maxHits = hitSprites.Length + 1;
        if (timesHit >= maxHits)
        {
           DestroyBlock();
        }
        else
        {
            shownexthitsprite();
        }
        
    }

    private void shownexthitsprite()
    {
        int spriteIndex = timesHit - 1;
        if (hitSprites[spriteIndex] != null)
        { 
        GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
        }
        else
        {
            
            Debug.LogError("Block Sprite is missing from array" + gameObject.name);
            
        }
    }

    private void DestroyBlock()
    {
      
        PlayBlockDestroySFX();
        Destroy(gameObject);
        level.BlockDestroyed();
        TriggerSparklexVFX();
        
    }

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

    }

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

Thank you.

Why can’t you assign a clip? Does the “Break Sound” field not appear in your Inspector? If it does appear, have you tried to assign another clip to the field? Does the clip itself work outside Unity? What clip did you use? Do you have a firewall or an antivirus program that could be blocking Unity?

no unity work fine and I can assign clip and apply to all block
but don’t know why this problem still appears the clip working fine and I get it from the course

((NullReferenceException: Object reference not set to an instance of an object))

Check that the break sound is dragged to the script in the unity inspector.
Its saying it cant find an object and is likely the sound clip hasnt been dragged in

already checked this is confuse me

Double click on the error message. To which line in your code does it refer? Could you paste the line here?

AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position);

There are two variables that could be null: breakSound or Camera.main.

Check if your Camera still has the MainCamera tag in its Inspector.

Hi Mohammed,

Could you possibly update us on this if the camera tag fixed the issue.
This usually happens if you have deleted the camera and added a new on in its place.

If no reply is received in 7 days then the topic will be archived.

Thanks in advance