Search Unity

Question Updating the names of small cubes to large cubes

Discussion in 'Scripting' started by julien83franceschi, Feb 10, 2024.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    232
    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:
    Code (CSharp):
    1.   if (mini == 0)
    2.         {
    3.            temp.x = 1.6f;
    4.             temp.y = 1.6f;
    5.             temp.z = 1.6f;
    6.             transform.localScale = temp;
    7.             mini = 1;
    8.         }
    9.     }
    To find out which cube has been clicked, I use the following code:


    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))    //  if I change with (Input.GetMouseButtonUp(0)) it works.
    4.         {
    5.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.  
    7.             RaycastHit hit;
    8.  
    9.             if (Physics.Raycast(ray, out hit))
    10.             {
    11.                 if (hit.collider.gameObject == gameObject)
    12.                 {
    13.                        Debug.Log("0");
    14.                 }
    15.                 else
    16.                 {
    17.                      Debug.Log("Cube cliqué : " + hit.collider.gameObject.name);
    18.                 }
    19.             }
    20.         }
    21.     }
    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+
     
    Last edited: Feb 10, 2024
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,420
    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.

    Code (CSharp):
    1.  
    2. public class GrowOnClick: MonoBehaviour
    3. {
    4.     public int mini = 0;
    5.  
    6.     //RECURSIVE
    7.     public GrowOnClick GetClickableRoot(Transform target)
    8.     {
    9.         if (target == null)
    10.             return null;
    11.         GrowOnClick clickable = target.GetComponent<GrowOnClick>();
    12.         if (clickable == null)
    13.             return null;
    14.         if (target.parent != null)
    15.         {
    16.             GrowOnClick parentClickable = GetClickableRoot(target.parent);
    17.             if (parentClickable != null)
    18.                 return parentClickable;
    19.         }
    20.         return clickable;
    21.     }
    22.  
    23.     public void GrowAllChildren()
    24.     {
    25.         foreach (GrowOnClick child in GetComponentsInChildren<GrowOnClick>())
    26.             Grow();
    27.     }
    28.  
    29.     public void Grow()
    30.     {
    31.         if (mini == 0)
    32.         {
    33.             //...
    34.             mini = 1;
    35.         }
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         if (Input.GetMouseButtonDown(0))
    41.         {
    42.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    43.             RaycastHit hit;
    44.             if (Physics.Raycast(ray, out hit))
    45.             {
    46.                 GrowOnClick clicked = GetClickableRoot(hit.collider.transform);
    47.                 if (clicked == null)
    48.                 {
    49.                        Debug.Log("no click");
    50.                 }
    51.                 else
    52.                 {
    53.                      Debug.Log("clicked: " + clicked.gameObject.name);
    54.                      clicked.GrowAllChildren();
    55.                 }
    56.             }
    57.         }
    58.     }
    59. }
    60.