In my game, when the first person controller jumps on a cube, the cube is supposed to disappear in 3 seconds. I used OnTriggerEnter function but I’m not sure on which object I have to put the script on.
Here is the script: (Note that this script is correct, I’ve tried it before but I just don’t remember on what object I used the script on.)
var Platform1 : GameObject;
function OnTriggerEnter (col : Collider)
{
print("Destroy!");
Destroy (Platform1.gameObject, 3);
}
I put another cube on top of the main cube that the player will jump on. This cube is inside the main cube that the player is jumping on but its mesh renderer box is unchecked so the player can land on the main cube. Picture: http://imgur.com/bVyHxxw
I’m not sure what I’m doing wrong here. I would appreciate if someone could guide me somehow.
You can use WaitForSeconds as an alternative. If you want the text to be printed on the screen you have to use two codes ( if you just want it in console you just use Debug.Log(“Destroy!”) ). I hope this isn’t to hard to follow.
First Code
var Platform1 : GameObject;
var PrintTxt = "" // What text you want printed
function OnTriggerEnter (col : Collider)
{
PrintTxt = "Destroy!";
yield WaitForSeconds(3.0); // The amount of time you want to wait before destroying
PrintTxt = ""; // This will make the variable an empty string
Destroy (Platform1.gameObject); // Simple Destroy Feature
}
Second Code ( Attach this to a gui text that you must create(to print text) and put it on the screen )
var targetScript: FirstCodeName; // Change FirstCodeName to your actual first code name and drag the object holding the script into this variable in inspector
function Update () {
guiText.text = targetScript.PrintTxt; // Use PrintTxt as that is the variable (Unless you change the variable name)
}
Goddamnit, My problem was THAT!? I just didnt check the boxes…im so mad…>.<
Your code also works perfectly so does mine. I actually was going to work on a text to be displayed on the screen but it seems that you took care of that for me! Thank you!!
If there are two objects, one a trigger and one the floor, make sure to delete both. You could do this by making an empty game object and making that the parent of multiple objects. You could then use the empty game object (which now has stuff in it) to destroy everything assigned to that object (eg. floor, trigger, light, etc.). Make sure to assign the platform1 variable to the empty game object if you do this.
Another way to check what is holding him up is to drag the game and scene objects near each other. As you run the game you can select objects which will show an indication of where they are and there central position. This will allow you to check what is interfering.
I still have the same problem. I did exactly what you said but nothing changes. I have my Platform1(which is a cube object) as a parent to a cube same size as Platform1 with its Mesh Renderer option unchecked. I have my script on this object and I dragged and dropped Platform1 into this script. The script works as intended but whenever I turn off one of the “is trigger” options on either object, the code does not work. But when It’s on as you said it should be, the first person controller just walks through it then after 3 seconds.