Updating the names of small cubes to large cubes

I’ve created a 3d rectangle (made up of 5 small cubes (1 parent and 4 children)).

When you click on one of the small cubes, the other 5 cubes expand into large cubes.

To do this, I’ll use the following code:

  if (mini == 0)
        {
           temp.x = 1.6f;
            temp.y = 1.6f;
            temp.z = 1.6f;
            transform.localScale = temp;
            mini = 1;
        }
    }

To find out which cube has been clicked, I use the following code:

void Update()
    {
        if (Input.GetMouseButtonDown(0))    //  if I change with (Input.GetMouseButtonUp(0)) it works.
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject == gameObject)
                {
                       Debug.Log("0");
                }
                else
                {
                     Debug.Log("Cube cliqué : " + hit.collider.gameObject.name);
                }
            }
        }
    }

Only when a small cube is clicked, do the small cubes enlarge, but the names of the large cubes are not updated (the names of the cubes in the 3D rectangle are 0, 1, 2, 3 or 4).

Why is the 3D rectangle update perfectly with if (Input.GetMouseButtonUp(0)) and not if (Input.GetMouseButtonDown(0))?

How can I update the large cubes once a small cube has been clicked?

Thanks for your help.

A+

You have a hierarchy:

  • parent

  • child

  • child

  • child

  • child

I think you want to make it so that a click on ANY child OR the parent to make ALL five objects grow. Is this correct?

Let’s say that your code is called GrowOnClick, a MonoBehaviour script.

If a Raycast finds a child collider, you should try to find the GrowOnClick component. If the transform has a parent, and it also has a GrowOnClick component, then you want to act as if you clicked on the parent instead. Then you ask the GrowOnClick component to make all of the children with GrowOnClick to grow at once.

The following is untested code.

public class GrowOnClick: MonoBehaviour
{
    public int mini = 0;

    //RECURSIVE
    public GrowOnClick GetClickableRoot(Transform target)
    {
        if (target == null)
            return null;
        GrowOnClick clickable = target.GetComponent<GrowOnClick>();
        if (clickable == null)
            return null;
        if (target.parent != null)
        {
            GrowOnClick parentClickable = GetClickableRoot(target.parent);
            if (parentClickable != null)
                return parentClickable;
        }
        return clickable;
    }

    public void GrowAllChildren()
    {
        foreach (GrowOnClick child in GetComponentsInChildren<GrowOnClick>())
            Grow();
    }

    public void Grow()
    {
        if (mini == 0)
        {
            //...
            mini = 1;
        }
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                GrowOnClick clicked = GetClickableRoot(hit.collider.transform);
                if (clicked == null)
                {
                       Debug.Log("no click");
                }
                else
                {
                     Debug.Log("clicked: " + clicked.gameObject.name);
                     clicked.GrowAllChildren();
                }
            }
        }
    }
}