- Home /
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);
}
}
}
Answer by whydoidoit · Jun 29, 2012 at 11:52 PM
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...
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroying a prefab on collision with a cube? 1 Answer
Delete 1 col.gameObject and not both 1 Answer
How do I make objects disappear as they are touched? 1 Answer
I can't get Destroy(collision.gameObject) to work? 2 Answers