x


Ignore awake function call to destroy game object if character enters trigger

Hey guys, running into a small issue here and I'm trying to find an effective way to resolve this. Upon awake I call the game object to be destroyed after 4 seconds. If the player enters the trigger of that game object he will take damage and a layer collision will be applied to prevent the player from taking more than one hit for 2 seconds at which point I reactivate the layer collision so the player can take damage again. I'm wondering if the reactivation of the layer collision is not taking place because the game object is destroyed before that reactivation is occurring. Anyone have any ideas? Maybe it's something simple. Here is the code. Thanks!

#pragma strict
#pragma downcast

var canHit = true;
var Hit : AudioClip;

function Awake() {
Destroy (gameObject, 4.0);
}

function OnTriggerEnter(other : Collider) {
var renderers = GetComponentsInChildren(Renderer);
if (gameObject != null) {
if (other.tag == "Parent" && canHit) {
other.SendMessage("Life",SendMessageOptions.DontRequireReceiver);
canHit = false;
other.audio.PlayOneShot(Hit);
for (var r : Renderer in renderers) {
    r.enabled = false;
}
Physics.IgnoreLayerCollision(9, 10, true);
yield WaitForSeconds(2);
Physics.IgnoreLayerCollision(9, 10, false);
Destroy(gameObject);
}
}
}
more ▼

asked Jun 29 '12 at 11:49 PM

stingman gravatar image

stingman
562 24 29 33

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

1 answer: sort voted first

Yes the coroutine will stop when the game object it destroyed. Instead make the latter part of the code a separate coroutine on the player and run it there...

more ▼

answered Jun 29 '12 at 11:52 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

Thanks Mike

Jun 29 '12 at 11:53 PM stingman
(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:

x2507
x2097
x764
x38
x1

asked: Jun 29 '12 at 11:49 PM

Seen: 427 times

Last Updated: Jun 29 '12 at 11:53 PM