Break Block Like Minectaft

Hey! im trying to do a minecraft like game. I have a script for breaking grassBlock, but i cant figureout how to make so it only breaks the grass blocks im looking at. I think i should use raycast but i dont know how to do so it detects tags. And should i have a different breakBlock script for every block or 1 script for all the block? because i want it to take different long time to break some blocks.

This is my BreakGrass script:

  public int BreakTimer = 3;
    public float Timer;
    public float Range;
    public Camera cam;
	
	
	
	void Update ()
    {
        if (Input.GetMouseButton(0))
        {
           
            

            Timer += Time.deltaTime;
            if(Timer >= BreakTimer)
            {
                BreakBlock();
                Timer = 0;
            }
        }
        if (Input.GetKeyUp(KeyCode.Mouse0))
        {
            Timer = 0;
        }

    }

     public void BreakBlock()
     {
        Destroy(gameObject);
     }

Hey there,

as @Somebody0 wrote you should follow his advice and read into raycasting in general.
As for the rest you should perhaps go on like this:

  1. create a basic “Block” class.
    This class will hold all basic information what a block can have.
    For example how long it should take to destroy it.
  2. give that script to all blocks that you create.
  3. When you raycast and hit something then the raycast will return you a gameobject.
  4. Check if your “Block” Component exists on this object.
  5. If yes access it and get the amount of time for destruction that you set on the blocks prefab beforehand.

Everything needed for this is described in the tutorial given by Somebody0 as well as the Scripting API behind this Link

Hope this helps.

You should learn something about Raycasting. Its way better. (I can’t paste there script because i’m on the phone).

Raycasting Unity Video - 3D Physics - Unity Learn