- Home /
New instances of object are colliding when collider is turned off.
My game's player object explodes on collisions with enemies (objects tagged enemy). It sends a message to an object which decrements lives and then instantiates the player prefab. This new player instance is supposed to have an invulnerability period during which the collider turns off, so that it doesn't hit anything until the period is up. The thing is, the new instances are colliding anyway. I've paused midgame to see that the collider is in fact, disabled. not just set to isTrigger, it's disabled totally. I've even tried destroying the collider, to no avail. The object is still colliding. It has 3 children, one of which has a collider that is also disabled (always), and two objects without colliders altogether. If I set the player object that is in the scene to begin with to have that invulnerability period (that is, check a bool newLife to true), it will not collide. it works as intended. It is only new instances that will do this. I'm really unable to figure out why these objects are colliding without colliders.
To be completely clear, during this invulnerability period, the collider is disabled. The box next to it is unchecked. Nothing I do in game, until that period ends, will turn it back on. The object acts as though the collider is enabled anyway, and only new instances do this. The object that exists within the scene on startup works properly.
Any help would be amazing. Thanks.
How exactly are you checking when the player collides with an enemy? In OnTriggerEnter()? onCollisionEnter()?
Answer by Doireth · Oct 10, 2013 at 09:15 PM
Why not add a simple flag to determine invulnerability?
void OnCollisionEnter(Collision col) {
if(!isInvulnerable) return;
// whatever normally happens during a collision
}
Whenever you die and respawn, set "isInvulnerable" to true. That way when other collisions occur, they do nothing. To reset "isInVulnerable" try
IEnumerator ResetInvulnerability() {
yield return new WaitForSeconds([WHAT EVER AMOUNT TO RESET YOU WANT]);
isInvulnerable = false;
}
When you die, start this coroutinue with:
StartCorouting(ResetInvulnerability());
That will turn off invulnerability after whatever amount of seconds you want so when "OnCollisionEnter" occurs again, normal collisions are calculated.
Thanks, this concept is working. I will need to get my enemies to not be destroyed on their collision with the player now while invulnerable, but I can figure that out, I believe. It might be neat to leave it that way too :P
Do you have any idea why my previous method would not work? I'm at a complete loss as to why it was behaving the way it was.
If your object has a rigidbody, the rigidbody counts all children rigidbodies as the same collider creating what is known as a "composite collider". Its possible that your children colliders might have been causing the problem. Honestly, it's been a long of coding and I skipped through most of your post :P
Hmm that could be it. Glad I got it worked out though, was really slowing me down. Thanks a lot.
Answer by ForrestS · Oct 10, 2013 at 09:04 PM
I'm using OnCollisionEnter() for all collision stuff. I'll be sure to note that's the case next time.
Your answer
Follow this Question
Related Questions
.sendmessage not working C# 1 Answer
why does Infinite Runner Terrain Collider break my game? 0 Answers
How to Instantiate GameObject on top of collider? (C# 1 Answer
Cannot get collision to do anything 1 Answer
TNT Damage Receiver 1 Answer