Bones do not switch when called for (Find bones of player then replace clothes bones)

Hello!
I have an issue with my script where as the bones that i assign and the bones that i get do not replace.

Basically my script is designed to replace the bones inside of specific clothing, in order to fit on my character. So the script gets all the bones from the skinned-meshrenderer of the clothe, finds all the bones inside of the player and then attempts to replace the bones assigned to the clothe with the playerbones, thus making the cloth act upon the player-bones. My issue is that after the all the bones have been found (both inside the clothe and the player), none of the bones are replaced. I’m not too sure what I am doing wrong as I have tested a previous script where this worked almost perfectly. My issue with the previous script is that the clothing that I want to use, uses multiple bones from my character. That script only let you assing one skinnedmeshrenderer (in my case, I have multiple skinnedmeshrenderers for my characters body), therefore making it not find the correct bones or leaving it with missing bones.

Here is my code:

void Start()
	{
		GameObject playerBones = GameObject.Find("ThirdPersonCharacter/Male/Male");     // Find the Parent of the bones
		Transform[] allChildren = playerBones.GetComponentsInChildren<Transform>();         // Get a list of all the children from the parent

		print("Object: " + playerBones);                                                                                // Displays which gameobject the script found

		Dictionary<string, Transform> playerboneMap = new Dictionary<string, Transform>();  //Creates a dictionary for the bones
		foreach (Transform child in allChildren)
		{
			playerboneMap[child.name] = child;
		}

		SkinnedMeshRenderer thisRenderer = GetComponent<SkinnedMeshRenderer>();
		Transform[] boneArray = thisRenderer.bones;
		for (int idx = 0; idx < boneArray.Length; ++idx)  //this checks if the bones match up, debugs an error if a bone is missing
		{
			string boneName = boneArray[idx].name;
			if (false == playerboneMap.TryGetValue(boneName, out boneArray[idx]))
			{
				Debug.LogError("failed to get bone: " + boneName);
				Debug.Break();
			}
		}
		thisRenderer.bones = boneArray; //take effect and replace the previous bones with the playerbones


		// Debugging, this will display all the children of the parent (the bones of the player)
		foreach (KeyValuePair<string, Transform> bone in playerboneMap)
		{
			print("Bone: " + bone.Key);
		}
	}

I really hope someone can help me out with this issue, as it will greatly increase my work on this game. :slight_smile:

Thanks in advance!

Something to add is that I have looked around on this forum for some similar results, yet no post has solved this issue. My code is based of one of the answers from this post

Yet I do believe that the error resolves around the thisRenderer.bones = boneArray; //take effect and replace the previous bones with the playerbones-part. But I’m not sure how I can assign the bones in any other way?