x


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

more ▼

asked Nov 30 '09 at 05:44 PM

zenislev gravatar image

zenislev
19 7 7 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
}
more ▼

answered Nov 30 '09 at 05:59 PM

Ricardo gravatar image

Ricardo
5.2k 20 32 96

Nice one. I probably should have mentioned that I am a complete code amateur though. Do you mind explaining how that would work? And how would I add that boolean in the script?

Nov 30 '09 at 09:01 PM zenislev

Actually, this seems to have worked - thanks to you both!

Dec 01 '09 at 05:23 PM zenislev

Glad to hear you got it working. As a recommendation, get some tutorials on programming - 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.

Dec 01 '09 at 06:03 PM Ricardo

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.

Dec 01 '09 at 06:17 PM zenislev
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 30 '09 at 10:06 PM

Grim420 gravatar image

Grim420
30 4 4 8

Thanks for that. Seriously, even the simple stuff seems impenetrable from a zero-knowledge standpoint. OCE has been a bane of mine for a while now. Good luck to you too =)

Dec 01 '09 at 05:25 PM zenislev
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2582
x1725

asked: Nov 30 '09 at 05:44 PM

Seen: 2142 times

Last Updated: Dec 01 '09 at 11:42 PM