Search Unity

Costume Follows Player After Removing It

Discussion in 'Scripting' started by artistshc, Jul 5, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Please Help. I just can't figure this one out and I want to cry like a little girl. :confused:

    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shirt : MonoBehaviour {
    5.  
    6.  
    7.     //ON BABY
    8.     //CHARACTER PICKING UP ITEM
    9.     void OnTriggerEnter2D(Collider2D other) {
    10.        
    11.         if (other.gameObject.name.Contains("Shirt")) {
    12.             if(DragClothes.isChilded == false){
    13.  
    14.                 //change position of the object to the character's right hand
    15.                 other.transform.position = this.transform.position;
    16.                 //parent the object to the child's right hand
    17.                 other.transform.parent = this.transform;
    18.                 this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";
    19.  
    20.             }
    21.         }
    22.     }
    23.    
    24.     //DROPPING ITEM
    25.     void OnTriggerExit2D (Collider2D other) {
    26.         if(other.gameObject.name.Contains("Shirt")){
    27.  
    28.             if(DragClothes.isChilded == true){
    29.                 other.transform.parent = null;
    30.                 //make it so that the object is NOT parented to the character any more
    31.                 DragClothes.isChilded = false;
    32.             }
    33.            
    34.         }
    35.     }
    36. }
    37.  
    Here is the code for DragClothes.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DragClothes : MonoBehaviour {
    5.  
    6.     public Renderer rend;
    7.     int originalSortOrder = 0;
    8.     bool blueNoContact = true;
    9.     public static bool isChalk = false;
    10.     public ItemTracker itemTracker;
    11.     public DirectionRaycasting2DCollider directionCollider;
    12.     Vector3 origScale;
    13.     public Transform Doll;
    14.     public static bool isChilded;
    15.  
    16.    
    17.    
    18.     //Drag and Drop  Method
    19.     void Start(){
    20.         //original scale of object
    21.         origScale = transform.localScale;
    22.         originalSortOrder = gameObject.GetComponent<Renderer> ().sortingOrder;
    23.         itemTracker = FindObjectOfType <ItemTracker>();
    24.         directionCollider = FindObjectOfType <DirectionRaycasting2DCollider>();
    25.         //set isChildred to default false
    26.         isChilded = false;
    27.  
    28.     }
    29.  
    30.     //<<----------------------------------------------------------------------------------//
    31.     //IF DRAGGING DOLL YOU DON'T WANT CLOTHES TO FALL OFF WHEN INTERACTING WITH OTHER DOLLS
    32.  
    33.     void OnTriggerEnter2D(Collider2D other) {
    34.         if(gameObject.transform.IsChildOf(Doll)){
    35.  
    36.             //now it is child of Doll
    37.             isChilded = true;
    38.        
    39.         }
    40.  
    41.     }
    42.  
    43.     void OnTriggerExit2D(Collider2D other) {
    44.  
    45.         if (gameObject.transform.IsChildOf (Doll)) {
    46.  
    47.             //now it is NOT child of Doll
    48.             isChilded = false;
    49.  
    50.         }
    51.  
    52.  
    53.     }
    54.  
    55.     //<<----------------------------------------------------------------------------------//
    56.  
    57.    
    58.     //SHRINK ITEMS THAT GO INTO CHEST
    59.     /*
    60.     void OnTriggerEnter2D(Collider2D other) {
    61.        
    62.  
    63.         if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
    64.             transform.localScale = new Vector3((0.5f * origScale.x),(0.5f * origScale.y),(0.5f * origScale.z));
    65.         }
    66.        
    67.        
    68.     }
    69.    
    70.     void OnTriggerExit2D(Collider2D other) {
    71.        
    72.         if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
    73.             //return to orginal size
    74.             transform.localScale = new Vector3( origScale.x,origScale.y,origScale.z);
    75.         }
    76.  
    77.     }
    78.    
    79.     //END SHRINK
    80.     */
    81.    
    82.     void OnMouseDrag()
    83.     {
    84.        
    85.         Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
    86.         point.z = transform.position.z;
    87.         transform.position = point;
    88.         //position the object a bit higher so that it is above other items when picking up
    89.         gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 250;
    90.         gameObject.GetComponent<Renderer> ().sortingLayerName = "Toppest";
    91.  
    92.        
    93.     }
    94.    
    95.     void OnMouseUp(){
    96.         directionCollider.checkOutOfCabinet (gameObject);
    97.         //if gameObject is no longer in box then return
    98.         if (itemTracker.itemsInBox.Contains (gameObject)) {
    99.             return;
    100.         } else { //else if gameobject is still in box then ...
    101.             //first make sure not one of the following...if so set sorting layer to default
    102.             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")){
    103.                 gameObject.GetComponent<Renderer> ().sortingLayerName = "Default";
    104.             } else {
    105.                 //Change sorting order of that item so that it is above stuff
    106.                
    107.                 gameObject.GetComponent<Renderer> ().sortingOrder = 200;
    108.                 gameObject.GetComponent<Renderer> ().sortingLayerName = "Top";
    109.                
    110.             }
    111.         }
    112.     }
    113. }
    114.  
    Please take a look if you could...and let me know where I am failing!!! I so appreciate it!!!
    Thanks...Rachel
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    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?
     
  3. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    I probably was just messing around with it because it wasn't working correctly. What should I do?
     
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Any ideas?
     
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Try using Debug.Log() or the debugger to see if the places where you set isChilded = false are actually reached.
     
  6. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Okay, thanks, I'll try that!
     
  7. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you for your help....figured it out. Here is the code for anyone who finds it helpful.

    DragClothes.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DragClothes : MonoBehaviour {
    5.  
    6.     public Renderer rend;
    7.     int originalSortOrder = 0;
    8.     bool blueNoContact = true;
    9.     public static bool isChalk = false;
    10.     public ItemTracker itemTracker;
    11.     public DirectionRaycasting2DCollider directionCollider;
    12.     Vector3 origScale;
    13.     public Transform Boy;
    14.     public Transform Baby;
    15.  
    16.     public bool isChildedBoy;
    17.     public bool isChildedBaby;
    18.  
    19.  
    20.    
    21.    
    22.     //Drag and Drop  Method
    23.     void Start(){
    24.  
    25.         //original scale of object
    26.         origScale = transform.localScale;
    27.         originalSortOrder = gameObject.GetComponent<Renderer> ().sortingOrder;
    28.         itemTracker = FindObjectOfType <ItemTracker>();
    29.         directionCollider = FindObjectOfType <DirectionRaycasting2DCollider>();
    30.         //set isChildred to default false
    31.         isChildedBoy = false;
    32.         isChildedBaby = false;
    33.  
    34.     }
    35.  
    36.     //<<----------------------------------------------------------------------------------//
    37.     //IF DRAGGING DOLL YOU DON'T WANT CLOTHES TO FALL OFF WHEN INTERACTING WITH OTHER DOLLS
    38.  
    39.     void OnTriggerEnter2D(Collider2D other) {
    40.         if(gameObject.transform.IsChildOf(Boy)){
    41.             Debug.Log ("Childed Boy");
    42.             //now it is child of Doll
    43.             isChildedBoy = true;
    44.        
    45.         }
    46.         if(gameObject.transform.IsChildOf(Baby)){
    47.             Debug.Log ("Childed Baby");
    48.             //now it is child of Doll
    49.             isChildedBaby = true;
    50.            
    51.         }
    52.  
    53.     }
    54.  
    55.     void OnTriggerExit2D(Collider2D other) {
    56.  
    57.         if (gameObject.transform.IsChildOf (Boy)) {
    58.             Debug.Log ("UnChilded Boy");
    59.             //now it is NOT child of Doll
    60.             isChildedBoy = false;
    61.  
    62.         }
    63.         if(gameObject.transform.IsChildOf(Baby)){
    64.             Debug.Log ("UnChilded Baby");
    65.             //now it is child of Doll
    66.             isChildedBaby = false;
    67.            
    68.         }
    69.  
    70.  
    71.     }
    72.  
    73.     //<<----------------------------------------------------------------------------------//
    74.  
    75.    
    76.     //SHRINK ITEMS THAT GO INTO CHEST
    77.     /*
    78.     void OnTriggerEnter2D(Collider2D other) {
    79.        
    80.  
    81.         if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
    82.             transform.localScale = new Vector3((0.5f * origScale.x),(0.5f * origScale.y),(0.5f * origScale.z));
    83.         }
    84.        
    85.        
    86.     }
    87.    
    88.     void OnTriggerExit2D(Collider2D other) {
    89.        
    90.         if ((other.gameObject.tag == "Cabinet") && (!(gameObject.transform.IsChildOf(Doll)))) {
    91.             //return to orginal size
    92.             transform.localScale = new Vector3( origScale.x,origScale.y,origScale.z);
    93.         }
    94.  
    95.     }
    96.    
    97.     //END SHRINK
    98.     */
    99.  
    100.  
    101.    
    102.     void OnMouseDrag()
    103.     {
    104.  
    105.         Vector3 point = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,(transform.position.z-Camera.main.transform.position.z)));
    106.         point.z = transform.position.z;
    107.         transform.position = point;
    108.         //position the object a bit higher so that it is above other items when picking up
    109.         gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 250;
    110.         gameObject.GetComponent<Renderer> ().sortingLayerName = "Toppest";
    111.  
    112.        
    113.     }
    114.    
    115.     void OnMouseUp(){
    116.         directionCollider.checkOutOfCabinet (gameObject);
    117.         //if gameObject is no longer in box then return
    118.         if (itemTracker.itemsInBox.Contains (gameObject)) {
    119.             return;
    120.         } else { //else if gameobject is still in box then ...
    121.             //first make sure not one of the following...if so set sorting layer to default
    122.             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")){
    123.                 gameObject.GetComponent<Renderer> ().sortingLayerName = "Default";
    124.             } else {
    125.                 //Change sorting order of that item so that it is above stuff
    126.                
    127.                 gameObject.GetComponent<Renderer> ().sortingOrder = 200;
    128.                 gameObject.GetComponent<Renderer> ().sortingLayerName = "Top";
    129.                
    130.             }
    131.         }
    132.     }
    133. }
    134.  
    Made a different shirt file for the boy and for the baby (so one for each character).

    ShirtBaby.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShirtBaby : MonoBehaviour {
    5.  
    6.     //SCRIPT IS ON DOLLS
    7.     public Transform Boy;
    8.     public DragClothes dragClothes;
    9.  
    10.  
    11.     void Start(){
    12.        
    13.         dragClothes = FindObjectOfType<DragClothes>();
    14.     }
    15.    
    16.    
    17.     //CHARACTER PICKING UP ITEM
    18.     void OnTriggerEnter2D(Collider2D other) {
    19.        
    20.         if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Boy)) {
    21.             if(dragClothes.isChildedBaby == false){
    22.                
    23.                 //change position of the object to the character's right hand
    24.                 other.transform.position = this.transform.position;
    25.                 //parent the object to the child's right hand
    26.                 other.transform.parent = this.transform;
    27.                 this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";
    28.                
    29.             }
    30.         }
    31.     }
    32.  
    33.     //DROPPING ITEM
    34.     void OnTriggerExit2D (Collider2D other) {
    35.         if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Boy)) {
    36.            
    37.             if(dragClothes.isChildedBaby == false){
    38.                 other.transform.parent = null;
    39.                 //make it so that the object is NOT parented to the character any more
    40.                 //dragClothes.isChilded = false;
    41.             }
    42.            
    43.            
    44.         }
    45.     }
    46.  
    47.  
    48.  
    49. }
    50.  
    Shirt.cs (goes on boy)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shirt : MonoBehaviour {
    5.  
    6.  
    7.     public Transform Baby;
    8.     //SCRIPT IS ON DOLLS
    9.  
    10.     public DragClothes dragClothes;
    11.  
    12.  
    13.     void Start(){
    14.  
    15.         dragClothes = FindObjectOfType<DragClothes>();
    16.     }
    17.  
    18.  
    19.     //CHARACTER PICKING UP ITEM
    20.     void OnTriggerEnter2D(Collider2D other) {
    21.         //if the item has the word shirt in its name AND isn't already childed to the baby then..
    22.         if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Baby)) {
    23.             if(dragClothes.isChildedBoy== false){
    24.  
    25.                 //change position of the object to the character's right hand
    26.                 other.transform.position = this.transform.position;
    27.                 //parent the object to the child's right hand
    28.                 other.transform.parent = this.transform;
    29.                 this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";
    30.  
    31.             }
    32.         }
    33.     }
    34.  
    35.  
    36.     //DROPPING ITEM
    37.     void OnTriggerExit2D (Collider2D other) {
    38.         if ((other.gameObject.name.Contains("Shirt")) && (other.gameObject.transform.parent != Baby)) {
    39.  
    40.             if(dragClothes.isChildedBoy == false){
    41.                 other.transform.parent = null;
    42.                 //make it so that the object is NOT parented to the character any more
    43.                 //dragClothes.isChilded = false;
    44.             }
    45.  
    46.            
    47.         }
    48.     }
    49.  
    50.  
    51.  
    52. }
    53.  
    Thanks again!