Search Unity

Best Way to Set Up Game with Multiple Characters and 1000s of objects

Discussion in 'Scripting' started by artistshc, May 7, 2015.

  1. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Hi there. I am making a dollhouse game. There isn't a winner or loser, it is just for little kids to play dollhouse with. I will have lots of characters for them to play with. They will be able to drag and drop them wherever they want. The child will also be able to drag and drop items on each character. I would then want the character to be able to walk around with these items. I want the object(s) to be parented to the character until the character drops the item. The character won't have a rigidbody because it will just be a flat sticker like image that can be moved - like Colorforms (if you remember what those are). I already have each character set up to drag and drop wherever. I placed a circle collider 2d on each hand. I want the child to be able to drag an item into the hand of whichever character they choose. That item should then stay in that hand until the child drags that item out of the hand. It should move around with the character.

    Thank you in advance for any support, advice, or code.
    Rachel
     
  2. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    If this question wasn't specific enough...maybe if someone could help me with the C# code for parenting one object to another. I'd probably be able to go from there. Thank you!
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Code (CSharp):
    1. oneObject.transform.parent = another.transform;
    Good luck with your project!
    - Joe
     
    Deleted User and artistshc like this.
  4. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you Joe!!! I attached it to a vase to test it ... here is the code ....


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PickupObject : MonoBehaviour {
    5.  
    6.     //we also want to know what the player is by accessing the BabyManager.cs script
    7.     private BabyManager baby;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         //find objects of type BabyManager - so find babies in the scene
    12.         baby = FindObjectOfType<BabyManager>();
    13.  
    14.     }
    15.  
    16.     void OnCollisionEnter (Collision other) {
    17.         if (baby != null){
    18.  
    19.             baby.transform.parent = other.transform;
    20.            
    21.  
    22.         }
    23.     }
    24. }
    25.  
    and it didn't work. Can you tell what I did wrong? I attached a screenshot for both the baby and the vase to show you how I have it setup. I appreciate you taking a look. ;-)

    THANK YOU!!!!
     

    Attached Files:

  5. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    OK, I got it to work if I do this...

    void OnTriggerEnter2D (Collider2D other) {
    if(other.gameObject.name == "Vase-Yellow"){
    other.transform.parent = GameObject.Find("RightHand").transform;
    }
    }

    However, it works whenever I collide with anything...I want it to work if I drag and drop an item on top of a character...only if it is dropped on the character. Any ideas? Thanks!!!
     
  6. Deleted User

    Deleted User

    Guest

    Lmao. I only read the thread title and then immediately after, your reply.
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hmm, I'm not sure. You're already checking other.gameObject.name, which seems like the right track to me. Think carefully about which object this script is on, and what the other object should be, and I bet you'll get it. (If you're like me, you'll probably wake up with the answer in the morning!)

    Failing that, you may have to just experiment — try the script on different objects, check for different names, until something clicks for you.
     
  8. artistshc

    artistshc

    Joined:
    Mar 7, 2015
    Posts:
    79
    Thank you for your help Joe. :) I did something like this and it works okay for now...

    Code (CSharp):
    1. //void OnCollisionEnter(Collision other){
    2.     void OnTriggerEnter2D (Collider2D other) {
    3.         if(other.gameObject.tag == "Pickable"){
    4.             Debug.Log ("Vase-Yellow");
    5.             other.transform.position = GameObject.Find("RightHand").transform.position;
    6.             other.transform.parent = GameObject.Find("RightHand").transform;
    7.  
    8.             //other.rigidbody.isKinematic = true;  
    9.         }
    10.     }
    11.  
    12.     //void OnCollisionEnter(Collision other){
    13.     void OnTriggerExit2D (Collider2D other) {
    14.         if(other.gameObject.tag == "Pickable"){
    15.             other.transform.parent = null;
    16.            
    17.             //other.rigidbody.isKinematic = true;  
    18.         }
    19.     }
    Thank you for your help.
    Rachel
     
    JoeStrout likes this.