0
\$\begingroup\$

I have a parent gameobject with rigidbody and parent script. The gameobject has many children and each child has Box collider and "is Trigger" option is on.

When each child collide with other object, it detected in parent script. But I can't detect which child colided...! 🧐

How can I figure it out without add script to children?

Children are too many and if I add any script to them the game performance get lowered.enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ A script with a collision handler on each child will not significantly impact performance if it has no Start/Awake/OnEnable/Update methods that need to be run on load/per frame. However, if the objects you're colliding with are less numerous than the children, you could invert the responsibility, putting a script on the object you're detecting to report up to the parent which child detected it. Is that an option in your case? \$\endgroup\$
    – DMGregory
    Jul 16, 2023 at 14:59
  • \$\begingroup\$ Also, are you sure you want an answer in UnityScript, a language that Unity hasn't supported since the 2010s? Or did you mean to tag this question C#? \$\endgroup\$
    – DMGregory
    Jul 16, 2023 at 18:50
  • \$\begingroup\$ @DMGregory Thanks for your answer. I use C# and the "unity script" tag was my mistake. The children are dynamic and maybe they are going too many. so I don't want attach any Monobehavior to them. I prefer to detect just by a single C# script in the parent. So is any way to detect which child collided? \$\endgroup\$
    – ESI Sonic
    Jul 18, 2023 at 10:39
  • \$\begingroup\$ Not a good one. Your options are scripts on the children, script on the thing(s) the children collide with, or iterating over the child objects calling overlap test methods on each one. \$\endgroup\$
    – DMGregory
    Jul 18, 2023 at 11:30

1 Answer 1

0
\$\begingroup\$

Add this script to the object(player) that collides with your triggers.

  private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.gameObject.name);
    }

And when your player collides the trigger it will display the trigger name.

\$\endgroup\$
4
  • \$\begingroup\$ That's incorrect ... I want to figured out which child colided ... not which other object. and I don't want add any script to childs ... coz children are too many and performance get lowered \$\endgroup\$
    – ESI Sonic
    Jul 18, 2023 at 10:33
  • \$\begingroup\$ ESI Sonic you've misunderstood this answer. This script goes on the other object that the children are colliding against, and successfully reports which child was involved in the collision. \$\endgroup\$
    – DMGregory
    Jul 18, 2023 at 11:32
  • \$\begingroup\$ Yes.. I know what that code do ... But as I said in the post, I don't want add any C# script to the other objects and children. And as you said, that problem does not have any way without add script to one of them. \$\endgroup\$
    – ESI Sonic
    Jul 19, 2023 at 18:38
  • \$\begingroup\$ Can I scripting without scripts? Can I use magic to display what game object was collided. \$\endgroup\$
    – Bogdan
    Jul 20, 2023 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .