- Home /
 
 
               Question by 
               AndersMalmgren · Apr 16, 2016 at 10:09 AM · 
                transformchild objectparent and childtransform.parent  
              
 
              Picking up object with transform.parent does not work
This is a start for my pickup script, its used to pickup objects in the world. WHen picked up it should be added as a child to the gameobject which the script is attached to. It does not work correctly, the object is attached, but its also transformed, its scale changes
 public class PickupObject : MonoBehaviour {
 
     private readonly List<GameObject> collidings = new List<GameObject> ();
     private GameObject pickedUpObject;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         print (collidings.Count);
         if (Input.GetMouseButtonDown (0) && pickedUpObject == null && collidings.Count != 0) {
             print("Picking up");
             pickedUpObject = collidings.First ();
             collidings.Clear();
             pickedUpObject.transform.parent = this.transform;
 
         } else if (Input.GetMouseButtonUp (0)&& pickedUpObject != null) {
             print("Dropping");
             pickedUpObject.transform.parent = null;
             pickedUpObject = null;
         }
     }
 
     void OnTriggerEnter (Collider other) {
         if (other.gameObject.tag == "Movable" && pickedUpObject == null) {
             collidings.Add(other.gameObject);
         }
     }
 
     void OnTriggerExit (Collider other) {
         if (other.gameObject.tag == "Movable") {
             collidings.Remove(other.gameObject);
         }
 
 
     }
 }
 
              
               Comment
              
 
               
              Answer by meat5000 · Apr 16, 2016 at 10:09 AM
Use SetParent instead of parent
http://docs.unity3d.com/ScriptReference/Transform.SetParent.html
It allows you to retain world position, scale and rotation.
Your answer