3D Object buttons - last one in the hierarchy is triggered every time

for an Android app, I have a GameObject with several children. Each of the children has a script attached and a mesh collider (with the related mesh) that turns them into buttons. Code:

    void Update()
    {
        if(Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            Ray ray = myCamera.ScreenPointToRay(touch.position);
            RaycastHit hitObject;
            if(Physics.Raycast(ray, out hitObject))
                {
                    GameObject hitGameObject = hitObject.transform.GetComponent<GameObject>();
                    TMPtext.text = "this: " + this.name;
                }

        }

    }

When I deactivate the other children, leave only one active, and tap on it, the correct name is displayed (it’s the name of the child I have tapped on). But when all children are active, whichever I tap, the name that is displayed is for the last object in the hierarchy. I don’t understand why. I’d appreciate your input.

Wouldn’t this be the issue?

Do you perhaps mean hitGameObject.name ?

1 Like

Yes I realized all the objects with that script are getting triggered and I should check the GO that’s hit not just “this”. Thanks for your help. I realized I was making a mistake but couldn’t delete the forum post.