Search Unity

Items Getting Un-childed from its Parents

Discussion in 'Scripting' started by artistshc, Jan 4, 2019.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Hi there. I have a doll house app and there are multiple "players". I have clothes that each doll can dress up in and it works perfectly with 1 doll. If you drag the doll with the clothes on, the clothes moves with the doll (the clothes is child-ed under the doll object). However if you drag the clothed doll over another doll, the clothes come off mid drag.

    This script is on each doll.

    Code (CSharp):
    1.     //CHARACTER WEARING HAT / GLASSES
    2.     void OnTriggerEnter2D(Collider2D other) {
    3.         //if other game object is a hat or glasses AND not dragging baby at the time then...
    4.         if (((other.gameObject.name.Contains("Hat")) || (other.gameObject.name.Contains("Glasses")) && (!(BabyManager.isDragging)))) {
    5.             //change position of the object to the character's right hand
    6.             other.transform.position = this.transform.position;
    7.             //parent the object to the child's right hand
    8.             other.transform.parent = this.transform;
    9.             //set to default sorting layer
    10.             this.transform.GetComponent<SpriteRenderer>().sortingLayerName = "Default";
    11.         }
    12.     }
    13.  
    14.     //DROPPING HAT / GLASSES
    15.     void OnTriggerExit2D (Collider2D other) {
    16.         if((other.gameObject.name.Contains("Hat")) || (other.gameObject.name.Contains("Glasses"))) {
    17.          
    18.             //make it so that the object is NOT parented to the character any more
    19.             other.transform.parent = null;
    20.          
    21.         }
    22.     }
    Any ideas? Thank you!
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    322
    Since you are moving them over another doll, my guess is that those collision checks are detecting the child objects' names and then the doll underneath is stealing them from the dragged doll.
     
    artistshc likes this.
  3. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Thank you, Wallace. I'll look into that.
     
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Can you think of any way that the item of clothing would be locked onto the first player?
     
  5. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    322
    Sure, something like this
    Code (csharp):
    1.     void OnTriggerExit2D (Collider2D other) {
    2.         if((other.gameObject.name.Contains("Hat")) || (other.gameObject.name.Contains("Glasses"))) {
    3.          
    4.             //make it so that the object is NOT parented to the character any more
    5.             if(other.transform.IsChildOf(this.transform)) // Only un-parent if we are looking at our own child
    6.                other.transform.parent = null;
    7.          
    8.         }
    9.     }
     
    artistshc likes this.
  6. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    70
    Thank you!!! I will try it! <3 I appreciate your help!
     
unityunity