For this tutorial, we are going to create a self-destructing cube object, and debug whether it has destroyed itself properly, along with the name of the cube in-game. This allows us to not only know that it is working, but if the script is applied to other objects, we will know which ones it is working on and when.
Create a new cube GameObject, and add it to the game world. Placement doesn't matter.
Create a new C# script and name it SelfDestruct.
Open the script in MonoDevelop.
Above the "void Start( ) { }" line, add a new string "public float duration = 1f;"
This makes a new public float called duration and sets it to be equal to 1.
Now we wont be using the "void Start( ) { }" function at all, so skip over it.
Inside your "void Update( ) { }" function we want to add a string that will decrease the "duration" float to 0. We do this by simply adding the line:
"duration -= Time.deltaTime;".
This tells our script to take the preset time (which we set to 1 already) and remove 1 for every second. You must add ".deltaTime" after the "Time" command, since "Time" is calculated per-frame; ".deltaTime" normalizes the loss per-frame over the course of real-world time.
*The function "duration -= Time;" would remove 1 from duration every frame, instead of every second.*
Now that we have a timer setup for duration, we need to tell the script what to do if the timer reaches 0.
We do this in the most simple way possible; using an "if" statement.
Below the duration timer line you just wrote, we want to create a new line:
"if (duration <= 0 ) { }".
This tells our script to do something (which we will define inside of the { }) if the timer is equal to or less than 0. You could add a script to make sure that the timer stops at 0, but since we are going to be destroying the object anyways, it would just be unnecessary scripting.
Now inside of the brackets, we want to tell our script what to do when the timer is 0 or less. Since we know we want to destroy our object, we will use the "Destroy" command.
Inside the { } brackets of your "if" statement, add the line:
"Destroy(gameObject);"
This tells the script to destroy whatever is inside the parentheses. In this case, we have defined that as "gameObject", which denotes the object the script is attached to.
We have now created a timer, setup a countdown for the timer, and told the script what to do if the timer reaches 0. Now we want to make sure that the Destroy function is working properly. So we have just one more line to add inside of the "if" statement brackets.
Below the "Destroy" line, add the following new line:
"Debug.Log("Destroyed: " + gameObject.name);
This line tells the console in Unity to display the message "Destroyed: " along with the name of the gameObject which was destroyed. This message will only appear once the "if" statement has run.
That's it, you should now be able save the script, and drag it to the cube you created and start the game. One second later you should see a message in your console that says "Destroyed: Cube" or whatever you named the cube. If you encounter an error, or the script doesn't work, make sure that it looks like this:
-------------------
using UnityEngine;
using System.Collections;
public class SelfDestruct : MonoBehaviour
{
public float duration = 1f;
void Update ()
{
duration -=Time.deltaTime;
if(duration <= 0)
{
Destroy(gameObject);
Debug.Log("Destroyed: " + gameObject.name);
}
}
}
-------------------
The code above works, and was written and tested by myself.
This is obviously a primitive implementation of the Debug.Log function, but it's an easy example, using real functionality. There are many different types of things you can Debug.Log; some of which have specific functions which can be called in different ways. None of which I'm going to discuss, as there are too many functions and uses to go in-depth about and I wanted this to be an easy tutorial.
The code above works, and was written and tested by myself.
This is obviously a primitive implementation of the Debug.Log function, but it's an easy example, using real functionality. There are many different types of things you can Debug.Log; some of which have specific functions which can be called in different ways. None of which I'm going to discuss, as there are too many functions and uses to go in-depth about and I wanted this to be an easy tutorial.

No comments:
Post a Comment