- Home /
Possible Unity BUG? Or am i missing something
Hey guys i recently ran into a problem and kinda stumped on why it acted this way. What happened was i have a gameobject with a box collider attached to it. In this gameobject i have a function to respawn the gameobject when it is killed which simply hides its render and moves it back to its starting position. Now the problem with it was that just hiding the renderer the gameobjects collider would still block incoming projectiles so i added to the respawn code to make the collider.active = false. Now when i did this my respawn code stopped performing its yield statement. Here is the code for reference in C#: float respawnTime = 5f; // How long to wait before respawning
void Start()
{ StartCoroutine(RespawnGameObject()); }
// Function to Respawn the GameObject private IEnumerator RespawnGameObject() { gameObject.collider.active = false; // Deactivate the gameObjects collider gameObject.renderer.enabled = false; // Hide the gameObjects
Debug.Log("Before the Yield");
yield return new WaitForSeconds(respawnTime);
Debug.Log("After the Yield");
gameObject.collider.active = true; // Reactivate the gameObjects collider
gameObject.renderer.enabled = true; // Make the gameObjects visible again
}
The above code is what i believe is possibly a bug or am i missing something? The debug statement "Before the Yield" is returned but not the "After the Yield". However with the code below both are returned:
float respawnTime = 5f; // How long to wait before respawning
void Start()
{ StartCoroutine(RespawnGameObject()); }
// Function to Respawn the GameObject private IEnumerator RespawnGameObject() { gameObject.collider.isTrigger= false; // CHANGED TO A TRIGGER gameObject.renderer.enabled = false; // Hide the gameObjects
Debug.Log("Before the Yield");
yield return new WaitForSeconds(respawnTime);
Debug.Log("After the Yield");
gameObject.collider.isTrigger= true; // CHANGED TO A TRIGGER
gameObject.renderer.enabled = true; // Make the gameObjects visible again
}
Now is this a bug or again am i missing something? I dont understand why disabling a collider would effect a yield statement. I have recreated the problem in a seperate project where i add this script to a cube and no other scripts are active and the trigger version works but the active does not. If anyone knows why this is please let me know! Thanks again
Answer by Mike 3 · Jan 09, 2011 at 10:28 PM
You're setting the GameObject inactive, not the collider (Component.active is a shortcut for setting the GameObject active status).
Since the GameObject is inactive, Unity doesn't call back into the coroutine
It should actually be causing a compilation warning about it being obsolete, along the lines of: the active property is deprecated on components. Please use gameObject.active instead. If you meant to enable / disable a single component use enabled instead.
though that isn't so useful for colliders, which don't have an enabled property
colliders have an isTrigger property, which will cange some things about it, but it is a way to "disable" a collider, at least the colliding part.
thanks for the quick answer mike, i dont recall any warnings but now that u mention it i do remember that being obsolete. Thanks again and now it makes sense!
Your answer
Follow this Question
Related Questions
Why does isTrigger = true not work without an update? 2 Answers
How do I pass through certain colliders but detect all collisions? 1 Answer
Can i make istrigger true just for spesific object ? 1 Answer
Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers
Is Trigger Collision Problem 1 Answer