- Home /
Spawn object problem
Hey how is the unity community doing today ive made an exploding car that replaces itself with a burnt on fire debris pile but when i blow the car up it spawns 7 times..... this is because of the explosion effect how would i got about making this only happen once or stopping it with out stopping my effect if it spawn into the blowing up car so i need them to stay together so i can throw the debris this is what i need to only run once
void Die (){
if (deadReplacement) {
Transform dead = Instantiate(deadReplacement, transform.position, transform.rotation) as Transform;
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
}
static void CopyTransformsRecurse ( Transform src , Transform dst ){
dst.position = src.position;
dst.rotation = src.rotation;
foreach(Transform child in dst) {
// Match the transform with the same name
Transform curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
UPDATE :
I figured out it was spawning as many times as my bullets hit it before it dies so when i shoot a shotgun at it it makes copys of how many bullets hit it when health hi zero still have the problem anyone have any ideas? on how to make it spawn once instantly and only once after explosion
Answer by Benproductions1 · Dec 21, 2013 at 01:27 AM
Hello,
Usually problems like these are caused because the specific event is fired multiple times per frame (or before the effect executes), causing multiple effects simultaneously. With the event being the death of your car and the effect being the instantiation of the deadReplacement
.
The usual way around the issue is to keep track of the objects state. The state is changed when the event is fired and is checked before the effect executes. In your case you would keep track of whether your car "is dead":
private bool isDead = false;
public void Kill() {
if (!isDead) {
isDead = true;
Die();
}
}
Hope this helps,
Benproductions1
ok just got this im rebooting my pc then ill try this ill let you know thanks
ok im having trouble with it im using this do get die to run
if (hitPoints <= 0.0f){
Send$$anonymous$$essage("Die");
i cant get it to run at all with the script you made it doesent play the effect either it cuts it short is there a way to make it toggle it on and then toggle right back off?
got just made it on a different layer and spawned it before the explosion
The code I wrote was a basic example of how it would be integrated into your code. It is not a separate script, nor have I tested it with any working example. It's just pseudocode (thats syntactically correct) and lends to explain the solution :)
Also, you should avoid using Send$$anonymous$$essage
Your answer
![](https://koobas.hobune.stream/wayback/20220613124558im_/https://answers.unity.com/themes/thub/images/avi.jpg)