Getting error when using GetComponent

Here is a shot of the error

Here is the rest of my code, please help!

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

public class Block : MonoBehaviour
{
// config params
[SerializeField] AudioClip breakSound; // add field to ball inspector to add the clip to play
[SerializeField] GameObject blockSparklesVFX;
[SerializeField] int maxHits;
[SerializeField] Sprite hitSprites;

// Cached reference
Level level;

// state variables
[SerializeField] int timesHit; // TODO only serialzed for debug purposes


private void Start()  // this runs once on game run
{
    CountBreakableBlocks();

}

private void CountBreakableBlocks()
{
    level = FindObjectOfType<Level>(); // finds and loads the class "level" to make it accessible to the block class and instantiates an instance of class level to variable level
    if (tag == "Breakable")
    {
        level.CountBlocks(); // call the function in the level class and increments the block count. So each block counts it's self.
    }
}


private void OnCollisionEnter2D(Collision2D collision) // OnCollistionEnter2D class of the collider  runs when a object collision happens
{
    if (tag == "Breakable")
    {
        HandleHit();
    }
}

private void HandleHit()
{
    timesHit++;
    if (timesHit >= maxHits)
    {
        DestoryBlock();
    }
    else
    {
        ShowNextHitSprite();
    }
}

private static void ShowNextHitSprite()
{
    int spriteIndex = timesHit - 1;
    GetComponent<SpriteRenderer>().sprite = hitSprites[spriteIndex];
}

private void DestoryBlock()
{
    
    
        
        Destroy(gameObject); // the destroy object has to be placed before the audiosource.playclipatpoint method or it destory won't work
    // breakSound is the audio clip to play. Camera.main.transform.position is 
    // where the camera is Position in world space from which sound originates.
        level.BlockDestroyed();
        TriggerSparklesVFX();
        PlayBlockDestroySFX();
        


}

private static void PlayBlockDestroySFX()
{
    FindObjectOfType<GameSession>().AddToScore();
    //AudioSource.PlayClipAtPoint(breakSound, Camera.main.transform.position); // Camera.main.transform.position is used because of the audio listner
}

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

}

Never mind, I found the issue I accidentally put static in the method
private static void ShowNexthitSprites()

It’s the simple errors that get anyone. Good you found the issue and fixed it.

Privacy & Terms