child object goes to another parent

Hello everyone,

I’m trying to grasp how to create transfer gameObject from one parent to another, but i’m just going in circles.
If someone could could help me over this little hump it would be greatly appreciated.
Example I’m trying to grasp below.

Character can pickup gameObject carry it and place it either on the ground (implemented) or inside another object (script call grave in this case). As far as I understand I have to access carriedObject from the CharacterController script somehow and change the object’s parent. All my attempts have failed so far.

Could you push me in the right direction?

using UnityEngine.EventSystems;
using UnityEngine;

public class CharacterController : MonoBehaviour
{
    public float speed;
    private Animator animator;
    private Vector2 boxSize = new Vector2(0.1f, 1f);
    public static GameObject carriedObject;

    private void Start()
    {
        animator = GetComponent<Animator>();
        carriedObject =  null;
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.F))
        {
            if( carriedObject != null )
            { // We're holding something already, we drop
                Drop();
            }
            else // Nothing in hand, we check if something is around and pick it up.
            {
                CheckInteraction();
            }  
        }

        Vector2 dir = Vector2.zero;
        if (Input.GetKey(KeyCode.A))
        {
            dir.x = -1;
            animator.SetInteger("Direction", 3);
        }
        //other movements here

        dir.Normalize();
        animator.SetBool("IsMoving", dir.magnitude > 0);

        GetComponent<Rigidbody2D>().velocity = speed * dir;
    }

    private void CheckInteraction()
    {
        //box casting instead of raycasting
        RaycastHit2D[] hits = Physics2D.BoxCastAll(transform.position, boxSize, 0, Vector2.zero);

        if(hits.Length > 0)
        {
            foreach(RaycastHit2D rc in hits)
            {
                if(rc.IsCarriable())
                {// PICK IT UP
                    carriedObject = rc.transform.gameObject;
                    if( carriedObject != null ) // Check if we found something
                    {
                        carriedObject.GetComponent<Rigidbody2D>().Sleep();
                        carriedObject.GetComponent<Rigidbody2D>().simulated=false;
                        carriedObject.GetComponent<SpriteRenderer>().sortingOrder++;
                        carriedObject.transform.parent = transform;
                        carriedObject.transform.localPosition = new Vector2( 0f, 1.2f);
                    }
                    return;
                }
                else
                {
                    rc.Interact();
                    return;
                }
            }
        }
    }

    private void Drop()
    {
        carriedObject.transform.localPosition = new Vector2( 0f, 0f);
        carriedObject.GetComponent<Rigidbody2D>().simulated=true;
        carriedObject.GetComponent<SpriteRenderer>().sortingOrder--;
        carriedObject.transform.parent = null;
       
        carriedObject = null; // Hands are free again
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grave : Interactable {

    private GameObject burriedObject;

    public override void Interact()
    {       
        base.Interact();
        //store carried object as a child of this object

        //HOT TO DO THIS?
    }
}

All parenting is done by either calling the .SetParent() method on the child’s Transform instance (best way giving you the most control), or by directly-manipulating the .parent field (of the child).

Hie thee to the Transform docs to see example code and possible uses.

1 Like