- Home /
Delayed Destroy Clone Not Working
I'm relatively new to Unity, and my shooting script is not working. I'm planning to shoot (instantiate a sphere) then destroy the sphere 5 seconds later. Without the following script, I can shoot, but the spheres will stay on screen. With it, or the Destroy(gameObject, float) on the instantiated sphere, the sphere is destroyed as soon as it is instantiated. Please help.
Here's the code:
if(Input.GetButtonDown("Shoot1"))
{ var OffsetCenter = transform.position + center; var instancesphere = Instantiate(sphere, OffsetCenter, transform.rotation); instancesphere.rigidbody.AddForce(transform.forward * sphereforce);
Destroy(instancesphere.gameObject, 5);
}
please format your code (edit -> select the code -> hit the 1010101 button); also, please don't use 'unity' as a tag (everything's about unity here... I fixed them)
Still not formatted. Sister$$anonymous$$y only fixed your tags for you (to get the unique Goddess of The Tags badge, which will be awarded to the first person to change more tag sets than there are Questions ;-)
Oooh... drools in her lap ... but I'll have to settle for 'Saint of Tags', as 'Goddess' goes against my religious believes ;)
Answer by Hendrys · Sep 02, 2011 at 08:54 PM
Simply add a new Script to the bullet prefab, and write on the Awake function:
Destroy(gameObject, 5);
That did the trick! Thanks! I had it all wrong in function update().
well, I guess that makes this the right answer. please click the 'accept' button (little green checkmark topleft of this answer)
Ok you are welcome ^^ , please mark it as the right answer.
This can't be the answer, it will destroy the object doing the instantiating.
Answer by SisterKy · Sep 02, 2011 at 08:34 PM
http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html should do what you want...? Greetz, Ky.
I removed my script component from the bullet and it shoots well, but the debugger keeps on saying "Can't Destroy Transform component. If you want to destroy the game object please call 'Destroy' on the gameobject ins$$anonymous$$d." I'm not sure how to do that in this case.
hrm... if you remove this script (which does destroy the gameObject, not the transform), but get this error that almost sounds like this script is not the problem but you have a Destroy() somewhere else. If you click on the error it should take you to the problematic code. Or if it doesn't for some reason, it's also telling you what path and file the error is at, as well as which line and column it's in...?
it seems that I wrongly put the script on the bullet with function update(). The problem's fixed now. Thanks though!
Your answer
Follow this Question
Related Questions
Why is Destroy(this.gameObject) not working immediately? 2 Answers
How can i get this Delay script to work? 1 Answer
How to create object falling from sky CONTINUOUSLY 1 Answer
Delay "Destroy(gameObject)" 2 Answers
destroy object after a delay? 6 Answers