- Home /
Tree Destroy Help
I need help on this script because the Destroy class wont work.
function OnTriggerEnter (other : Collider)
{
if(other.tag == "Player")
{
if(Input.GetKeyDown("c"))
{
Destroy(gameObject);
}
renderer.material.color = Color.gray;
}
}
for further help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting. When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it.
I have formatted this one for you. Please check the code is the same as what you have.
This is asking that you press the "c" key at the exact moment the trigger is entered, that would be fairly impossible. You can test if Destroy is working by removing the if(Input.Get$$anonymous$$eyDown("c")) part (actually the object should still be turning grey). For what you seem to be doing, you need to store a reference to the last object that the trigger collided with, and the if "c" is pressed destroy that object.
Answer by SirWaffles · Jan 05, 2013 at 07:59 PM
Yes it is the same, the script is for when you walk into the area of the object, when i press "c" it gets destroyed
Your answer
![](https://koobas.hobune.stream/wayback/20220613090433im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to destroy an object at a specific position? 2 Answers
Destroy Prefab using Javascript 2 Answers
Destroying a gameobject clone after a time 3 Answers
auto destruct a particle system. 3 Answers
What is wrong with my script? 1 Answer