- Home /
Can't add SkinnedMeshRenderer with AddComponent()?
So in the game I am creating, I am destroying a subobject (e.g an arm) which has a SkinnedMeshRenderer and then creating a new object which should have the same SkinnedMeshRenderer (new object will be attached to the same armature), but after doing the following line of code:
go.AddComponent<SkinnedMeshRenderer>();
I am getting the following error:
Destroying components immediately is not permitted during physics trigger/contact, animation event callbacks, rendering callbacks or OnValidate. You must use Destroy instead.
UnityEngine.GameObject:AddComponent<UnityEngine.SkinnedMeshRenderer> ()
This happens on collision.
Given the error message, I doubt AddComponent
is responsible for the error.
However, I suspect you're calling DestroyImmediate(go.GetComponent<XXX>())
instead.
@Hellium If I remove the AddComponent the error disappears. Nevertheless, I don't have any DestroyImmediate() in the code. I just added this one line and the error started happening.
Does go
already have any kind of renderer? (MeshRenderer / SkinnedMeshRenderer / ...)
Yes, but I have tried doing
Destroy(go.GetComponent<MeshRenderer>())
before the AddComponent
and I have the same result, with or without it.
It doesn't have a SkinnedMeshRenderer already, but it does have it after the AddComponent
Answer by Bunny83 · Nov 02, 2021 at 09:03 AM
Regardless of your error, do you know that if the mesh you're trying to render is an actual skeletal animated mesh, the SkinnedMeshRenderer needs to have the proper bone array. This array is created during import and stored inside the SkinnedMeshRenderer. It's the glue between the Mesh and the bone structure. It has to match 100%.
Note: Just because to seperate meshes have the same bone structure does not mean that the order of the bones in the bones array is the same. This could even happen when the mesh is reimported. So if you have multiple seperate skinned meshes with the same bone structure, you have to remap the bones anyways. Since this is quite a bit of work anyways, there's usually no need to destroy and re-add the SkinnedMeshRenderer. You just have to replace the sharedMesh and set the bones array correctly.
To address your error, it's hart to tell how this error was actually produced without any context. From the error message we can assume that you do this inside:
physics trigger/contact, animation event callbacks, rendering callbacks or OnValidate
Those are all callbacks from within certain internal processes in Unity and you should not destroy objects in those callbacks. You could delay the actual replacement with a coroutine that waits one frame for the next update.
It's possible that when you use the normal "Destroy" which usually delays the destruction until the end of the current frame, when you immediately add a new component to the same place, Unity is forced to remove the old one immediately. So it would make sense that Destroy alone works, but Destroy with adding the new object does not.
Though there were a lot of speculations here since we don't really have much context.
ps: If you need a way to remap the bones properly, your bones need tohave unique names and you can use the extension method shown over here.
I put the AddComponent in a co routine, which waits for a frame to end (and hence the original MeshRenderer to be destroyed with Destroy) before adding the new SkinnedMeshRenderer - This makes the error go away. You were right that Destroy was waiting for the frame to end so AddComponent was technically called before the Destroy and doing its own immediate Destroy of the MeshRenderer, which is not allowed in a Collision trigger.
I am sorry for the ambiguity of my question and thank you very much for your extensive answer!
It's actually an interesting edge case I haven't come across in all those years ^^.
Your answer
Follow this Question
Related Questions
Why does adding cloth component to skinned meshrenderer move the bounds away from the mesh? 2 Answers
Combining Skinned Meshes no animation 0 Answers
My animation will not play after I apply armature in Blender. 1 Answer
SkinnedMeshRenderer with no animation drops FPS 0 Answers
Modifying SkinnedMeshRenderer.sharedMesh.uv Doesnt Reset after Stopping game. 1 Answer