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

[C#] attach GameObject as a children to another GameObject

Discussion in 'Scripting' started by pKallv, May 3, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I have been working with this for hours looking at all kinds of suggestions on the net and in this forum but not got this to work. I am very new to Unity and C# so i am still learning. I guess i am still a bit hooked to how this is done in Objective-C and SpriteKit.

    This is the scenario:

    I use this piece of code to catch all objects on a specific location.
    Code (csharp):
    1. RaycastHit2D[] hits = Physics2D.LinecastAll(clickedPos,clickedPos,theLayer);
    I use the following code to create an empty "container" (GameObject) that i want too use as the parent:
    Code (csharp):
    1. // Create a container for the selected sprites using the ContainerObject prefab
    2. containerGO = Resources.Load("ContainerObject") as GameObject;
    3. containerGO.tag = "Container";
    4. containerGO.transform.position = hits[0].transform.position; // Set position at clicked object
    5. Instantiate(containerGO);
    I then have been reading and tested all kinds of things to try to move the selected raycast-"hits" to become a child to containerGO but not succeeded. I am aware that i need to care about the direct link to the prefab, got an error message, but just can't figure out how to properly do this.

    The following is the code i am currently use and it does not work:
    Code (csharp):
    1. //>>>>>>TESTING<<<<<<//
    2.  
    3. foreach(RaycastHit2D theChild in hits) {
    4.     string theChildName = theChild.transform.tag + "(Clone)"; //(Clone) is for the prefabs that are currently on the game board
    5.  
    6.     dummyGameObject = GameObject.Find(theChildName);
    7.  
    8.     gameObjectList.Add(dummyGameObject);
    9. }
    10.  
    11. //The 'gameObjectList[1]' is just to test with a predefined object in hits so I do not need to iterate
    12. Transform aChildPiece = gameObjectList[1].transform;   //containerGO.transform; // This is parent node
    13.  
    14. GameObject clone = Instantiate(aChildPiece, transform.position, transform.rotation);
    15.  
    16. clone.transform.parent = containerGO.transform;
    17.  
    18. //>>>>>>TESTING<<<<<<//
    I need all help i can get :-(
     
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    parenting is done via the Transform Component

    theChild.transform.parent = containerGO.transform;
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I tried that:
    Code (csharp):
    1. clone.transform.parent = containerGO.transform;
     
  4. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Im not sure what you are doing incorrect then as this is how its done. I've done it hundreds of times without any problems. Are both objects in the scene? can you see them there?

    Edit :


    Also what error are you mentioning , if the runtime throws an error in a script it could cause other pieces of code to not execute..
     
    Last edited: May 3, 2014
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks shaderbytes for the help :)

    uhhh I am not very good at this...

    I solved it by placing this code on the object:
    Code (csharp):
    1. void OnLongPress(LongPressGesture gesture) {
    2.         print ("Here we go - " + MainScript.selectedGameObjectList.Count);
    3.  
    4.         firstRound = true;
    5.  
    6.         // Allocate all selected objects to the container
    7.         foreach(GameObject aGameObject in MainScript.selectedGameObjectList) {
    8.             print ("aGameObject: " + aGameObject.name);
    9.             if(firstRound) {
    10.                 // At the first round do not add child as the first object is parent
    11.                 print ("The first round" + aGameObject.name);
    12.                 theFirstGO = aGameObject;
    13.  
    14.                 firstRound = false;
    15.             }
    16.             else {
    17.                 // Add to parent
    18.                 //print ("The other rounds: " + aGameObject.name);
    19.  
    20.                 aGameObject.transform.parent = theFirstGO.transform;
    21.                 aGameObject.collider2D.enabled = false;            
    22.             }
    23.         }