- Home /
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?
Answer by vonlindberg · Mar 07, 2017 at 04:21 PM
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);
}
}
Answer by Ragee · Jun 29, 2020 at 08:52 AM
Those functions indeed makes the mesh visible but I get a new issue as seen in picture below:
Answer by ShawnRG · Dec 03, 2020 at 11:31 AM
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);
}
Answer by MagyarPeter · Sep 25, 2021 at 02:47 PM
public class CopyBones : MonoBehaviour
{
private void Awake()
{
SkinnedMeshRenderer smr = GetComponent<SkinnedMeshRenderer>();
print(transform.parent);
SkinnedMeshRenderer example = transform.parent.GetComponent<Example>().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.
I used this script to add clothes to my character via code, it worked
Your answer
Follow this Question
Related Questions
is it possible to affect bones on skineed mesh by wind? 2 Answers
Mesh goes invisible after modifying vertices 2 Answers
Objects Not Visible In Low End Computer. (Shader Error) 1 Answer
Dynamically instantiated skinned mesh not playing animations 4 Answers
Problems importing plane mesh from blender to unity 1 Answer