简体   繁体   中英

Unity 3D Strange Behaviour Attaching Loaded Prefab to Object

Hey Everyone could use some advice. On running my program I'm loading a prefab and attaching it to a bone of a character object.

The character is called "MrPresident" and it has an attach point called "genSuit_AttachHandRight". Through another script I call this function to load a prefab called 'basicBriefcase', attach it to this attach point, and attempt to set its position to (0, 0, 0) relative to the attach point.:

public void Equip(string mode)
{
    // on startup load basic equipment
    if ( mode == "Initialize" )
    {
        // right hand equipment
        string modelRightAttachPoint = championProperties.modelRightAttachPoint;
        string defaultRightHandType = championProperties.defaultRightHandType;
        string defaultRightHandObject = championProperties.defaultRightHandObject;
        if ( defaultRightHandType == "custom")
        {
            string championName = this.name;
            GameObject equipment = Instantiate(Resources.Load("Equipment/" + championName + "/" + defaultRightHandObject, typeof(GameObject))) as GameObject;
            equipment.name = defaultRightHandObject; // gets rid of the (Clone)

            equipment.transform.parent = GameObject.Find(modelRightAttachPoint).transform;
            equipment.transform.position = Vector3.zero;
        }
    }
}

The prefab is loading in the Hierarchy and appears to be attached to the attach point when I press play, but its nowhere to bee seen in the Game view. When I try debugging by running this:

public void Update()
{
    print(GameObject.Find("basicBriefcase").transform.position);
}

The value is flying all over the place. The first line of output is (0, 0, 0) on the first Update, but the position after that its changing every cycle through update. The character its attached to is animated but its just sitting in place, and I would think the position relative to the attach point should remain constant at what i set it. If I remove the line

equipment.transform.parent = GameObject.Find(modelRightAttachPoint).transform;

Then the position stays at (0, 0, 0), but because i havent attached to the approprite parent (the attach point) the position is obviously wrong. Am I missing something here? Thanks for your help.

EDIT

I discovered that i should be using

equipment.transform.localPosition = Vector3.zero;

instead of .position but now I'm having a whole new problem. Where before the equipment was attaching ok to the parent, now it just dissapears when I try to set the parent.

If i execute this statement

GameObject equipment = Instantiate(Resources.Load(startPath + defaultRightHandObject, typeof(GameObject))) as GameObject;

The object appears in the hierarchy with no parent. But if I try to do:

Transform trans = GameObject.Find("MrPresidentAttachRightHand").transform;     
equipment.transform.SetParent(trans, true);

The object just dissapears. Its nowhere to be found, its not attached to the parent it should be attached to. I've tried using .parent and .SetParent. I've tried reloading the prefab it is supposed to attach to. If i try to set the parent to something like Camera.main.transform, it works. If i set it to the highest parent in the prefab i'm trying to attach it to, it works. But if i try to attach it to the attach point, it dissapears. What am I doing wrong?

Just don't use transform.parent to set the parent of a GameObject. Use the transform.SetParent function which let's you specify if the object should be place relative to the parent.

That should be:

Transform trans = GameObject.Find(modelRightAttachPoint).transform;
equipment.transform.SetParent(trans, true);

If you still have issue, pass false to it:

Transform trans = GameObject.Find(modelRightAttachPoint).transform;
equipment.transform.SetParent(trans, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM