- Home /
Trigger in children object activates function in parent
My prefab is a sphere with an empty game object (that has a trigger) attached to it. It is supposed to represent a bomb.
The way it is supposed to work: The prefab is thrown at an enemy. When the "bomb" makes contact with another gameObject that has the tag enemy it sends a message to the empty game object. Then, the empty object destroys all the gameObjects with the "enemy" tag within its trigger.
The way it works: The prefab is thrown at an enemy. When an enemy touches the trigger of the empty object the message that destroys the gameObjects is sent.
I recently updated my project from unity 3 to 4. Everything was working correctly before the update. I haven't changed anything in the code but my object behaves differently, now.
The script from the sphere:
public class BombCollision : MonoBehaviour {
// When an enemy hits the bomb, a message is sent to the child object that has the tigger attached
void OnTriggerEnter (Collider collider) {
// Debug.Log ("Hit something that is not an enemy");
if (collider.gameObject.tag == "Enemy") {
// Debug.Log ("Hit the bomb");
BroadcastMessage("EngageTargets");
StartCoroutine("timeForExplision");
}
}
// gives time for BombKiller to destroy the objects are within the collider before destroying the bomb
IEnumerator timeForExplision () {
yield return new WaitForSeconds(0.05f);
Destroy(gameObject);
}
The script from the empty game object attached to the sphere:
public class BombKiller : MonoBehaviour {
bool contact = false;
void OnTriggerStay (Collider collision) {
if (collision.gameObject.tag == "Enemy" && contact == true) {
collision.SendMessage("WasHit");
}
}
void EngageTargets () {
Debug.Log ("got engageTargets");
contact = true;
}
}
I've tried to solve this for a whole day now, I hope someone can help me out, thanks for reading.
Hi !
Check this thread : http://forum.unity3d.com/threads/26194-Compound-Colliders-and-Trigger-Scripts
If your parent have a rigidbody, and the children gets collided, the parent trigger is called.
I also did not had this issue before upgrading to Unity 4... looking into it.
Thanks for your reply! I was able to work around this issue by setting the child collider to inactive and then setting it to active when an enemy collided with the parent. I hope you were able to solve the issues upgrading to Unity 4 caused. Sorry it took so long to reply I moved and did not have internet for almost a month!
No problem! Glad I could help :) Also yes I resolved it, but by using 2 rigidbodies !