- Home /
Multiple clones instantiating upon collision - I only want one, how do I stop this?
I've got an 'infection' system set up, which is supposed to work like this: 'uninfected' GameObject collides with 'infected' GameObject; uninfected GameObject is destroyed and an infected clone instantiated in its place.
The problem is, I've noticed that more than one clone is being instantiated sometimes. I think this might be because, since the player character is always infected, the uninfected GameObjects often collide with two separate infected GameObjects at once and the code spawns an infected clone for each of those collisions - instead of just one - before destroying the original uninfected GameObject. Here's the code I'm using:
var follower : GameObject;
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.name == "Player" || collision.gameObject.name == "FollowerInfectedPrefab" || collision.gameObject.name == "FollowerInfectedPrefab(Clone)"){
KillSelf();
} }
function KillSelf () {
var followerInfected = Instantiate(follower, transform.position, transform.rotation);
Destroy(gameObject);
}
Any ideas how to stop this from happening? I only want one infected clone per uninfected GameObject :S
Answer by Ricardo · Nov 30, 2009 at 05:59 PM
Add a boolean "spawned" that is initially false, and alter KillSelf() to something like this:
function KillSelf () { if (spawned) return; spawned = true; var followerInfected = Instantiate(follower, transform.position, transform.rotation);
Destroy(gameObject);
}
Nice one. I probably should have mentioned that I am a complete code amateur though. Do you $$anonymous$$d explaining how that would work? And how would I add that boolean in the script?
Actually, this seems to have worked - thanks to you both!
Glad to hear you got it working. As a recommendation, get some tutorials on program$$anonymous$$g - regardless of if they're C# or not. You're bound to run into some non-trivial things soon enough, where the community won't be able to help you with quick snippets.
I absolutely agree. I had intended to just play around with Unity, but it's impressed me to the point of actively wanting to learn. I can't keep on scavenging for code forever.
Answer by Grim420-2 · Nov 30, 2009 at 10:06 PM
You can declare a variable to be a boolean at the beginning of the script by using:
var spawned : boolean = false;
I'm pretty new to this code stuff too, but I'm also having similar collision problems. The one thing we have in common is the OnCollisionEnter (seems to like repeating things you don't want it to), and doing this same kind of thing didn't help me. I'm actually still stuck on this, good luck to you though.
Thanks for that. Seriously, even the simple stuff seems impenetrable from a zero-knowledge standpoint. OCE has been a bane of $$anonymous$$e for a while now. Good luck to you too =)
Your answer
![](https://koobas.hobune.stream/wayback/20220613173144im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
"Destroy" bullet without turning off trail renderer 2 Answers
Instantiating a Particle system always facing Z-axis 1 Answer
Attaching a joint on the instantiated object and connecting it with the collided object. 1 Answer
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer