Bug with adding clothes to character's body (twist mesh)

For characters customization I using the simple script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ClothesSimpleScript : MonoBehaviour {
        public GameObject m_attached;

        void Start () {    
                SkinnedMeshRenderer targetRenderer = m_attached.GetComponent<SkinnedMeshRenderer>();
                Dictionary<string, Transform> boneMap = new Dictionary<string, Transform>();
                foreach( Transform bone in targetRenderer.bones )
                        boneMap[bone.name] = bone;

                SkinnedMeshRenderer thisRenderer = GetComponent<SkinnedMeshRenderer>();
                Transform[] boneArray = thisRenderer.bones;
                for(int idx = 0; idx < boneArray.Length; ++idx )
                        string boneName = boneArray[idx].name;
                        if( false == boneMap.TryGetValue(boneName, out boneArray[idx]) )
                                Debug.LogError("failed to get bone: " + boneName);
                thisRenderer.bones = boneArray; //take effect

Nothing is satisfactory until I came to the import into the project of the clothing that belongs to the legs, ie trousers, pants and so on. They adding to the body of the character buggy. Right leg terribly twisted. Everything else works. Jackets, shirts, etc. also work without problems.

Here pants, standing by themselves (not added to the body of the character):

But the pants in the game, has added to body using script (disabled visibility of the character body, to better see the pants). As you can see, right leg twisted:

What’s the matter?! I verifies the correctness of the names of all the bones, thought, where can the letter accidentally added a letter - in pants and torso completely identical skeletons! At the same time a female character that’s all right, bug happening only with men’s clothes (weird, huh?). Pick up the script buggy Men’s pants and drop this on a woman’s body (skeletons of the all characters is identical) - the same problem! Pants twists on female and the male bodys equally. If I do not use a script, and add pants on scene, add them animator controller, they will normally run, jump, without bugs. It is necessary to apply script - again there is a bug (with any feet-clothing: pants, shorts, etc) … What do you think?

P.S. Link to my project- https://yadi.sk/d/FhZ0SXNBsjjHM

A lot of warping bugs happen when you add things after the initial load in. It might be better to have them already attached and then enable them during the script. I had some models that I instantiated weapons into their hands via code. The same thing happened to me. The weapons were warped when the armature would move. When adding a gameObject as a child to any other gameObject, you have to make sure that the parent’s transform is zeroed out, other wise the warping will occur because you’re applying it to a gameObject whose transform is already transformed, therefore the child takes on that same behavior in a very ugly way. Again, add them in the editor and activate/deactivate through script. It’ll save you some time.