Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to swap hair of a character with another hair and it won't bind to parent

Discussion in 'Scripting' started by Rastilan, Feb 11, 2015.

  1. Rastilan

    Rastilan

    Joined:
    Apr 21, 2013
    Posts:
    4
    Not sure whats going on, trying to follow many guides or related questions on the subject. However it copies the transform and then just sits there. Since it doesn't become a child of the object it doesn't follow the object the way the original hair gameobject did. The script is a little messy due to me just trying to get the new object to parent to the resident object.

    using UnityEngine;
    using System.Collections;

    public class room001HairController : MonoBehaviour {

    public GameObject newHair;
    public GameObject currentHair;
    public bool destroyHair;
    public GameObject femaleResident;
    public Transform currentHairTransform;

    // Use this for initialization
    void Start () {
    femaleResident = GameObject.FindGameObjectWithTag ("Resident");
    currentHair = GameObject.FindGameObjectWithTag ("Hair");
    currentHairTransform = currentHair.transform;

    }

    // Update is called once per frame
    void Update () {
    ReplaceHair ();

    }

    void ReplaceHair()
    {
    if (Input.GetKeyDown (KeyCode.O)) {
    Instantiate(newHair);
    currentHairTransform.transform.parent.gameObject = femaleResident.transform.gameObject;
    // set position
    newHair.transform.position = currentHair.transform.position;

    // set rotation
    newHair.transform.rotation = currentHair.transform.rotation;

    // set scale
    newHair.transform.localScale = currentHair.transform.localScale;
    if (destroyHair)
    Destroy (currentHair);
    }
    }
    }
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You parenting code is not quite right. You also need a reference to the new hair instance
    Code (CSharp):
    1. Gameobject newHairInstance = Instantiate(newHair) as GameObject;
    2. newHairInstance .transform.parent = femaleResident.transform;
     
  3. Rastilan

    Rastilan

    Joined:
    Apr 21, 2013
    Posts:
    4
    So it has to be created first? I had the gameobject added in the inspector. Ill try this though, and see what I can get working
     
  4. Rastilan

    Rastilan

    Joined:
    Apr 21, 2013
    Posts:
    4
    Seems to be working! Now I just gotta make it follow the transform perfectly and figure out why it instantiates invisible. Thanks!
    Posting working code for anyone interested. Still need to clean it up of course.

    using UnityEngine;
    using System.Collections;

    public class room001HairController : MonoBehaviour {

    public GameObject newHair;
    public GameObject currentHair;
    public bool destroyHair;
    public GameObject femaleResident;
    public Transform currentHairTransform;
    public GameObject newHairInstance;

    // Use this for initialization
    void Start () {
    newHairInstance = Instantiate(newHair) as GameObject;
    femaleResident = GameObject.FindGameObjectWithTag ("Resident");
    currentHair = GameObject.FindGameObjectWithTag ("Hair");
    currentHairTransform = currentHair.transform;

    }

    // Update is called once per frame
    void Update () {
    ReplaceHair ();

    }

    void ReplaceHair()
    {
    if (Input.GetKeyDown (KeyCode.O)) {
    Instantiate(newHair);

    // set position
    newHair.transform.position = currentHair.transform.position;

    // set rotation
    newHair.transform.rotation = currentHair.transform.rotation;

    // set scale
    newHair.transform.localScale = currentHair.transform.localScale;
    newHairInstance.transform.parent = femaleResident.transform;
    if (destroyHair)
    Destroy (currentHair);
    }
    //currentHairTransform.transform.parent.gameObject = femaleResident.transform.gameObject;
    }
    }