Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help with prefabs and children

Discussion in 'Scripting' started by doodoo_peehole, Sep 12, 2023.

  1. doodoo_peehole

    doodoo_peehole

    Joined:
    Aug 12, 2020
    Posts:
    2
    Hi!

    I'm trying to make a system with cubes that can be placed on other cubes and they get deleted if collecting a cube means it's floating, if that makes sense (see video for an example lol). This works by instantiating a cube prefab and setting it as the child of whatever cube it's placed on so that when one gets destroyed, all it's children get destroyed with it.

    I'm having a problem with the placement of the cubes. If I place a cube on a cube that already has a child it will also duplicate the cube and place cubes on each of it's children.

    I have the code and a video to help show what I mean (definitely not optimised yet I know that lol). Does anyone have a way to stop the placement code activating on all the children cubes? i.e: only activate the code on the cube the player is looking at. Thanks for any help!

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(1))
    2.         {
    3.  
    4.             RaycastHit hit;
    5.  
    6.             if (Physics.Raycast(transform.position, transform.forward, out hit, pickingRange))
    7.             {
    8.  
    9.                 string tag = hit.transform.tag;
    10.  
    11.                 if (tag == "PlaceHere")
    12.                 {
    13.  
    14.                     if (inventory[pointer] > 0)
    15.                     {
    16.                         hit.transform.GetComponent<GenerateBlock>().Generate(pointer);
    17.                         inventory[pointer]--;
    18.                     }
    19.                 }
    20.             }
    21.         }
    Code (CSharp):
    1. public void Generate(int cubeType)
    2.     {
    3.  
    4.         cube = Instantiate(cubeTypes[cubeType], transform.parent.transform.position + transform.up, new Quaternion(0,0,0,0));
    5.  
    6.         if (transform.parent.tag == "Scaffold")
    7.         {
    8.             cube.transform.parent = transform.parent;
    9.             cube.GetComponent<BlockFunctionByType>().cubeType = cubeType;
    10.         }
    11.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
  3. doodoo_peehole

    doodoo_peehole

    Joined:
    Aug 12, 2020
    Posts:
    2

    oops sorry should've said but that snippet of code is in Update(), so surely every frame it checks for the mouse being clicked right? I'll see if using OnMouseDown() helps but Unity already registers a mouse click - the issue is with duplicated instantiations.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    That probably won't be as useful to you as simply setting aside what you have and doing a click-and-drag tutorial. There's like ten billion out there for 3D stuff, give a few of them a try, save yourself some time and frustration.