Script for mining blocks in Unity

Hi folks,

I’m currently programming my own Minecraft in Unity (it’s name is Mikacraft), but I’ve the following problem:

In a YouTube-video the programmer dragged a script (the script is to mine blocks) where it says “SampleScene”. I’ve tried to drag this script there, but in my project I can’t drag the script where it says “SampleScene” (I currently use version 2020.2.0f1 and the programmer has version 5.6.2f1), but the script is very IMPORTANT for my game! Is there perhaps a detour to use this script in the project without dragging it to where it says “SampleScene”? Here is the code of the script:

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

public class BlockScript : MonoBehaviour {
    // Start is called before the first frame update
    void Start() {
       
    }

    // Update is called once per frame
    void Update() {
       
    }
   
    void OnMouseDown() {
        Destroy(this.gameObject);
    }
}

It would be really nice for me, if you can help me.

Sincerely yours,
Atten007

SampleScene would be one of your scenes. You cant drag a script directly into a scene. You can only attach it to a GameObject inside a scene. Also, calling Destroy in OnMouseDown will literally destroy all blocks, since you have not clarified which block to destroy. All blocks with that script attached will simply wait for a MouseDown event and then destroy themselves. Normally you’d want another script to handle this, like the player, who casts a ray and then either destroys the object that ray hits, or not.

I dont intend to sound rude, but from the way you write it seems you are very inexperienced. Unless you want just some couple-hundred-block prototype, recreating a game like Minecraft is actually a very advanced thing to do. Even ignoring the complexity of generating a world itself, the major hurdle is performance optimizations.
Using Minecraft as an example, a single chunk can have up to 16x16x256 = 65536 blocks. Just having that many objects in your scene will reduce your FPS to a slideshow, even if they do absolutely nothing yet.
A blocky world like Minecraft actually does not consist of even a single block. It’s one mesh (per chunk) that looks like it is made of a lot of blocks. So instead of 65536 objects per chunk, you have one. It’s also a bit more complicated than to just combine the meshes, since you will want to remove all unseen surfaces (ie all surfaces that do not touch ‘air’). Not to mention that you will need a way to edit this single mesh in a way that seems like you are adding or removing blocks from it. For this you usually save the data of each chunk in a simple array containing informations about the blocks to display, and then have a function that builds the mesh for this underlying data structure.
Then there is the problem that the function rendering the chunk based on its underlying data structure will not realistically run in a single-threaded game, so adding multithreading to this all is pretty much necessary.

None of these are exactly beginner friendly topics. I’m not saying this to discourage you, but you should know what you are getting into. If you are like me an dont care about the complexity of the topic as long as it interrests you, take baby steps. Read a couple papers, watch a couple tutorials, and expect to sit on it for a couple weeks at least. Otherwise maybe postpone this project until you feel more comfortable and come back to it later.

Hi Yoreki,

I have now decided to create a game object for dismantling blocks, but since I only know a little C #, I don’t know what exactly I have to change in the source code. Can you please help me with what I need to change / add / remove? Here is my C # source code again:

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

public class BlockScript : MonoBehaviour {
    // Start is called before the first frame update
    void Start() {
      
    }

    // Update is called once per frame
    void Update() {
      
    }
  
    void OnMouseDown() {
        Destroy(this.gameObject);
    }
}

What should I change in the source code when I want it to be that the destruction of blocks is in a game object? I ask you that because I only know a bit C#.

LG Atten007

In this case the simplest thing would be to learn more C#. If you have difficulties with a tutorial chosse an easier one. Minecraft like game is not the one you should do first. Start with simpler things like breakout, roll a ball etc… You have to learn proper programming as you can’t ask the forums for every error/problem you have. So sit down and do learning resources until you feel confident to tackle such an ambitious project. When you have such problems with practically empty scripts you won’t come very far. Learning the stuff you need can’t be done by anyone else but you.

And I suggest to learn C# programming independently from Unity first. C# yellow book is a good and free ebook which teaches you most concepts. When you understand that learn Unity.

1 Like

You should definitely NOT destroy yourself.

Instead, you should always strive to IMPROVE yourself!

In the area of Unity3D, these tutorial makers are super-good:

Imphenzia / imphenzia - super-basic Unity tutorial:

Jason Weimann:

Brackeys super-basic Unity Tutorial series:

Sebastian Lague Intro to Game Development with Unity and C#:

Imphenzia: How Did I Learn To Make Games:

How to do tutorials properly:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That’s how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Kurt, reported you for necroing and spamming. Just kidding ;), but this thread is done IMO. Op hasn’t been seen for half a year so I guess he gave up. But your advise could still be usefull for others finding this thread. Just you know to pick the “fights” that count and make a difference.

1 Like