Costume Follows Player After Removing It

Please Help. I just can’t figure this one out and I want to cry like a little girl. :face_with_spiral_eyes:

So I am able to put the costume on the players

Here is a pic of before I put the costumes on…

here is the players with the costumes on…

I can move the players around with the costumes…it works like a charm. However, if I take the costume of the player and place it on the ground, then move the player around, the costume moves around with the player (because it is still a child of the player). Here is a pic to illustrate what I mean…

Here are the settings of the player’s head (which I have put the code on - the code C# file is shirt.cs)

And here is settings on each of the costumes

Here is the code:

Here is Shirt.cs

using UnityEngine;
using System.Collections;

public class Shirt : MonoBehaviour {


    //ON BABY
    //CHARACTER PICKING UP ITEM
    void OnTriggerEnter2D(Collider2D other) {
       
        if (other.gameObject.name.Contains("Shirt")) {
            if(DragClothes.isChilded == false){

                //change position of the object to the character's right hand
                other.transform.position = this.transform.position;
                //parent the object to the child's right hand
                other.transform.parent = this.transform;
                this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";

            }
        }
    }
   
    //DROPPING ITEM
    void OnTriggerExit2D (Collider2D other) {
        if(other.gameObject.name.Contains("Shirt")){

            if(DragClothes.isChilded == true){
                other.transform.parent = null;
                //make it so that the object is NOT parented to the character any more
                DragClothes.isChilded = false;
            }
           
        }
    }
}

Here is the code for DragClothes.cs

using UnityEngine;
using System.Collections;

public class DragClothes : MonoBehaviour {

    public Renderer rend;
    int originalSortOrder = 0;
    bool blueNoContact = true;
    public static bool isChalk = false;
    public ItemTracker itemTracker;
    public DirectionRaycasting2DCollider directionCollider;
    Vector3 origScale;
    public Transform Doll;
    public static bool isChilded;

   
   
    //Drag and Drop  Method
    void Start(){
        //original scale of object
        origScale = transform.localScale;
        originalSortOrder = gameObject.GetComponent<Renderer> ().sortingOrder;
        itemTracker = FindObjectOfType <ItemTracker>();
        directionCollider = FindObjectOfType <DirectionRaycasting2DCollider>();
        //set isChildred to default false
        isChilded = false;

    }

    //<<----------------------------------------------------------------------------------//
    //IF DRAGGING DOLL YOU DON'T WANT CLOTHES TO FALL OFF WHEN INTERACTING WITH OTHER DOLLS

    void OnTriggerEnter2D(Collider2D other) {
        if(gameObject.transform.IsChildOf(Doll)){

            //now it is child of Doll
            isChilded = true;
       
        }

    }

    void OnTriggerExit2D(Collider2D other) {

        if (gameObject.transform.IsChildOf (Doll)) {

            //now it is NOT child of Doll
            isChilded = false;

        }


    }

    //<<----------------------------------------------------------------------------------//

   
    //SHRINK ITEMS THAT GO INTO CHEST
    /*
    void OnTriggerEnter2D(Collider2D other) {
       

        if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
            transform.localScale = new Vector3((0.5f * origScale.x),(0.5f * origScale.y),(0.5f * origScale.z));
        }
       
       
    }
   
    void OnTriggerExit2D(Collider2D other) {
       
        if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
            //return to orginal size
            transform.localScale = new Vector3( origScale.x,origScale.y,origScale.z);
        }

    }
   
    //END SHRINK
    */
   
    void OnMouseDrag()
    {
       
        Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
        point.z = transform.position.z;
        transform.position = point;
        //position the object a bit higher so that it is above other items when picking up
        gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 250;
        gameObject.GetComponent<Renderer> ().sortingLayerName = "Toppest";

       
    }
   
    void OnMouseUp(){
        directionCollider.checkOutOfCabinet (gameObject);
        //if gameObject is no longer in box then return
        if (itemTracker.itemsInBox.Contains (gameObject)) {
            return;
        } else { //else if gameobject is still in box then ...
            //first make sure not one of the following...if so set sorting layer to default
            if ((gameObject.name.Contains("Shirt")) || (gameObject.name.Contains("Hat")) || (gameObject.name == "Bowl") || (gameObject.name == "Bowl2") || (gameObject.name == "Bowl3") || (gameObject.name == "Bowl4") || (gameObject.name == "Plate") || (gameObject.name == "Plate2") || (gameObject.name == "Plate3") || (gameObject.name == "Plate4") || (gameObject.name == "Cup") || (gameObject.name == "Cup2") || (gameObject.name == "Cup3") || (gameObject.name == "Cup4")){
                gameObject.GetComponent<Renderer> ().sortingLayerName = "Default";
            } else {
                //Change sorting order of that item so that it is above stuff
               
                gameObject.GetComponent<Renderer> ().sortingOrder = 200;
                gameObject.GetComponent<Renderer> ().sortingLayerName = "Top";
               
            }
        }
    }
}

Please take a look if you could…and let me know where I am failing!!! I so appreciate it!!!
Thanks…Rachel

you only change the parenting of ‘other’ (the shirt) if DragCloths.isChilded is true, but you can’t guarantee that it is set first, since both scripts contain an OnTriggerExit2D block
Why are you even setting it to false in both scripts?

I probably was just messing around with it because it wasn’t working correctly. What should I do?

Any ideas?

Try using Debug.Log() or the debugger to see if the places where you set isChilded = false are actually reached.

Okay, thanks, I’ll try that!

Thank you for your help…figured it out. Here is the code for anyone who finds it helpful.

DragClothes.cs

using UnityEngine;
using System.Collections;

public class DragClothes : MonoBehaviour {

    public Renderer rend;
    int originalSortOrder = 0;
    bool blueNoContact = true;
    public static bool isChalk = false;
    public ItemTracker itemTracker;
    public DirectionRaycasting2DCollider directionCollider;
    Vector3 origScale;
    public Transform Boy;
    public Transform Baby;

    public bool isChildedBoy;
    public bool isChildedBaby;


   
   
    //Drag and Drop  Method
    void Start(){

        //original scale of object
        origScale = transform.localScale;
        originalSortOrder = gameObject.GetComponent<Renderer> ().sortingOrder;
        itemTracker = FindObjectOfType <ItemTracker>();
        directionCollider = FindObjectOfType <DirectionRaycasting2DCollider>();
        //set isChildred to default false
        isChildedBoy = false;
        isChildedBaby = false;

    }

    //<<----------------------------------------------------------------------------------//
    //IF DRAGGING DOLL YOU DON'T WANT CLOTHES TO FALL OFF WHEN INTERACTING WITH OTHER DOLLS

    void OnTriggerEnter2D(Collider2D other) {
        if(gameObject.transform.IsChildOf(Boy)){
            Debug.Log ("Childed Boy");
            //now it is child of Doll
            isChildedBoy = true;
       
        }
        if(gameObject.transform.IsChildOf(Baby)){
            Debug.Log ("Childed Baby");
            //now it is child of Doll
            isChildedBaby = true;
           
        }

    }

    void OnTriggerExit2D(Collider2D other) {

        if (gameObject.transform.IsChildOf (Boy)) {
            Debug.Log ("UnChilded Boy");
            //now it is NOT child of Doll
            isChildedBoy = false;

        }
        if(gameObject.transform.IsChildOf(Baby)){
            Debug.Log ("UnChilded Baby");
            //now it is child of Doll
            isChildedBaby = false;
           
        }


    }

    //<<----------------------------------------------------------------------------------//

   
    //SHRINK ITEMS THAT GO INTO CHEST
    /*
    void OnTriggerEnter2D(Collider2D other) {
       

        if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
            transform.localScale = new Vector3((0.5f * origScale.x),(0.5f * origScale.y),(0.5f * origScale.z));
        }
       
       
    }
   
    void OnTriggerExit2D(Collider2D other) {
       
        if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
            //return to orginal size
            transform.localScale = new Vector3( origScale.x,origScale.y,origScale.z);
        }

    }
   
    //END SHRINK
    */


   
    void OnMouseDrag()
    {

        Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
        point.z = transform.position.z;
        transform.position = point;
        //position the object a bit higher so that it is above other items when picking up
        gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 250;
        gameObject.GetComponent<Renderer> ().sortingLayerName = "Toppest";

       
    }
   
    void OnMouseUp(){
        directionCollider.checkOutOfCabinet (gameObject);
        //if gameObject is no longer in box then return
        if (itemTracker.itemsInBox.Contains (gameObject)) {
            return;
        } else { //else if gameobject is still in box then ...
            //first make sure not one of the following...if so set sorting layer to default
            if ((gameObject.name.Contains("Shirt")) || (gameObject.name.Contains("Hat")) || (gameObject.name == "Bowl") || (gameObject.name == "Bowl2") || (gameObject.name == "Bowl3") || (gameObject.name == "Bowl4") || (gameObject.name == "Plate") || (gameObject.name == "Plate2") || (gameObject.name == "Plate3") || (gameObject.name == "Plate4") || (gameObject.name == "Cup") || (gameObject.name == "Cup2") || (gameObject.name == "Cup3") || (gameObject.name == "Cup4")){
                gameObject.GetComponent<Renderer> ().sortingLayerName = "Default";
            } else {
                //Change sorting order of that item so that it is above stuff
               
                gameObject.GetComponent<Renderer> ().sortingOrder = 200;
                gameObject.GetComponent<Renderer> ().sortingLayerName = "Top";
               
            }
        }
    }
}

Made a different shirt file for the boy and for the baby (so one for each character).

ShirtBaby.cs

using UnityEngine;
using System.Collections;

public class ShirtBaby : MonoBehaviour {

    //SCRIPT IS ON DOLLS
    public Transform Boy;
    public DragClothes dragClothes;


    void Start(){
       
        dragClothes = FindObjectOfType<DragClothes>();
    }
   
   
    //CHARACTER PICKING UP ITEM
    void OnTriggerEnter2D(Collider2D other) {
       
        if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Boy)) {
            if(dragClothes.isChildedBaby == false){
               
                //change position of the object to the character's right hand
                other.transform.position = this.transform.position;
                //parent the object to the child's right hand
                other.transform.parent = this.transform;
                this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";
               
            }
        }
    }

    //DROPPING ITEM
    void OnTriggerExit2D (Collider2D other) {
        if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Boy)) {
           
            if(dragClothes.isChildedBaby == false){
                other.transform.parent = null;
                //make it so that the object is NOT parented to the character any more
                //dragClothes.isChilded = false;
            }
           
           
        }
    }



}

Shirt.cs (goes on boy)

using UnityEngine;
using System.Collections;

public class Shirt : MonoBehaviour {


    public Transform Baby;
    //SCRIPT IS ON DOLLS

    public DragClothes dragClothes;


    void Start(){

        dragClothes = FindObjectOfType<DragClothes>();
    }


    //CHARACTER PICKING UP ITEM
    void OnTriggerEnter2D(Collider2D other) {
        //if the item has the word shirt in its name AND isn't already childed to the baby then..
        if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Baby)) {
            if(dragClothes.isChildedBoy== false){

                //change position of the object to the character's right hand
                other.transform.position = this.transform.position;
                //parent the object to the child's right hand
                other.transform.parent = this.transform;
                this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";

            }
        }
    }


    //DROPPING ITEM
    void OnTriggerExit2D (Collider2D other) {
        if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Baby)) {

            if(dragClothes.isChildedBoy == false){
                other.transform.parent = null;
                //make it so that the object is NOT parented to the character any more
                //dragClothes.isChilded = false;
            }

           
        }
    }



}

Thanks again!