x


OnCollisionEnter seemingly triggers twice instantly on Instantiated objects.

Edited to hopefully show up in more responses: I have a script that is intended to check an objects tag upon any collision and if it meets my conditions then destroy both objects and spawn in a new bigger one in the same place as the first object that collided. My problem is that instead of two smaller objects becoming one larger one as intended, I end up with two larger ones.

function OnCollisionEnter(hit : Collision) {

bubChangePos = this.transform.localPosition; //the position to spawn at.

if (bubSize == 0 && hit.gameObject.tag == "Bubble0")
{
	Destroy(hit.gameObject);
	Destroy(this.gameObject);
	Instantiate (bub1Prefab, bubChangePos, Quaternion.identity);
	bubSize = 1;
}
}

I thought that the bubSize var would stop it from triggering again since it would no longer be 0 but that and a bunch of other checks and balances I've tried have failed. It seems like it's going to call OnCollisionEnter twice no matter what.

Please help out with this specific script and be succinct. If I get a JavaScript answer that solves my issue I will happily mark it as solved and upvote it if you can refrain from using the words "simple", "simply", and the phrase "Mouthbreathing Idiot!". :)

--Goody!

more ▼

asked Dec 13 '09 at 04:47 AM

Goody! gravatar image

Goody!
516 24 31 46

I removed the javascriptspecific tag since the question is relevant regardless of language choice.

Feb 17 '10 at 01:01 PM runevision ♦♦
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If the script is attached to both objects, it will be run twice when the collide, once on each object, because each object detects the collision independently. Try checking not just the bubSize of the object the script is, but also the bubSize of the other object.

more ▼

answered Dec 13 '09 at 04:57 AM

Stelimar gravatar image

Stelimar
3k 14 16 50

Yup, you got it! I'm new at Instantiate and didn't figure out that both objects are calling on collision at the same time. Doh! I've now moved the qualifying vars outside of the original script and don't have the same problem anymore. Thanks! Solved! Upvoted!

Dec 14 '09 at 01:42 AM Goody!
(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:

x5080
x1874

asked: Dec 13 '09 at 04:47 AM

Seen: 1776 times

Last Updated: Feb 17 '10 at 12:59 PM