Search Unity

Question Run-time duplicating of Skinned Mesh?

Discussion in 'Scripting' started by Yandalf, Jan 1, 2022.

  1. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Hey everyone!

    I'm trying to make a little script that temporarily copies a Skinned Mesh Renderer at runtime to apply a different material to for a visual effect. I'm running into some problems with it however.

    Right now, I create a new GameObject as a sibling to the existing Skinned Mesh Renderer object. This copy is given a SMR of its own, given the same sharedMesh and having the rootBone and localBounds configured based on the orginal SMR. I also copy the original object's transform and apply this to the copy.
    This results in nothing being visible however, when selecting the copied object I don't get any kind of outline in the scene view either.

    Weirdly enough, if I then copy the original component and paste its values into the copied component, the model does show up and animates properly. What step am I missing here?
    Code below just to be clear.
    Code (CSharp):
    1.         var copy = new GameObject("SkinnedMeshCopy_Wireframe").AddComponent<SkinnedMeshRenderer>();
    2.         copy.transform.SetParent(creature.transform);
    3.         copy.transform.SetPositionAndRotation(creature.Renderer.transform.position, creature.Renderer.transform.rotation);
    4.         copy.transform.localScale    = creature.Renderer.transform.localScale;
    5.         copy.gameObject.layer        = creature.gameObject.layer;
    6.         copy.sharedMesh                = creature.Renderer.sharedMesh;
    7.         copy.localBounds            = creature.Renderer.localBounds;
    8.         copy.rootBone                = creature.Renderer.rootBone;
    9.         copy.sharedMaterial            = settings.WireframeMaterial;
     
  2. TheFunnySide

    TheFunnySide

    Joined:
    Nov 17, 2018
    Posts:
    200
    1) If your copy has the orginal as parent then setting its local position and rotation to 0 should make it overlap.
    SetPositionAndRotation has unexpected consequences the way you use it.
    2) Making a copy of an object by transferring all public fields is not a guarantied way to make an exact copy.
    There could be internal states which are hidden from you but are essential for the behavior of the class.
    Unity provided https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
     
  3. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    Yes, overlapping is intentional in this case. That is not the problem as the transform component is in the correct position.
    Instantiating does do the trick, however in many cases it might be an imperfect solution (I just want a duplicate of the animated mesh in this case, so any of the other components in the duplicated hierarchy are unwanted and might cause issues). I assume there's indeed some hidden state, I'd just hoped there was a solution for it.