Cant see mesh of instantiated skinned mesh renderer

Hey,

So I’ve been working on a character customiser for my game, and when I instantiate a prefab with a skinned mesh renderer (as a child to a socket of a character template) I cannot see the mesh. It has a standard diffuse shader applied to it, and I can select the game object and see its bounds, but I cant see the mesh. What could be the cause of this?

This happens because the SkinnedMeshRenderer needs references to the bones, and these references are broken when you instantiate it from a prefab.

You need to set the “rootBone” and “bones” properties of the SkinnedMeshRenderer component after instantiating it. The latter is an array of transforms which can be built from the root bone using the following functions:

    Transform[] BuildBonesArray (Transform rootBone)
	{
		List<Transform> boneList = new List<Transform> ();
		ExtractBonesRecursively (rootBone, ref boneList);
		return boneList.ToArray ();
	}

	void ExtractBonesRecursively (Transform bone, ref List<Transform> boneList)
	{
		boneList.Add (bone);

		for (int i = 0; i < bone.childCount; i++)
		{
			ExtractBonesRecursively (bone.GetChild (i), ref boneList);
		}
	}

Those functions indeed makes the mesh visible but I get a new issue as seen in picture below:

I had the same issue, this was because the order of the bones added to the array is important.
The first thing I did is to take a prefab where the SkinnedMeshRenderer rendered correctly, added a small script to it to print out the SkinnedMeshRenderer.bones array. I then saw that the order was completely different. I then just reordered the bone hierarchy to match the one in the working prefab.

Note: Also make sure to skip disabled bones by wrapping the ExtractBonesRecursively call in an if statement

         var child = bone.GetChild(i);
         if (child.gameObject.activeInHierarchy)
         {
            ExtractBonesRecursively (child, ref boneList);
         }

public class CopyBones : MonoBehaviour
{
private void Awake()
{
SkinnedMeshRenderer smr = GetComponent();
print(transform.parent);
SkinnedMeshRenderer example = transform.parent.GetComponent().example;
smr.rootBone = e.rootBone;
smr.bones = e.bones;
}
}

public class Example : MonoBehaviour
{
public SkinnedMeshRenderer example;
}

//Put the “Example” script on the root of the character(not the armature, the parent of the body mesh for example).

The “Example” script’s “example” will be a skinned mesh renderer that is always on the character, and works perfectly(so it wasn’t instantiated from script), assign it manually.

Put the “CopyBones” script to the SkinnedMeshRenderer that you instantiated with script.

The “CopyBones”'s gameobject’s parent has to contain the “Example” script.