• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by filkata123 · Nov 01, 2021 at 03:47 PM · animationmeshmeshrendererskinnedmeshrendererarmature

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.

Comment
Bunny83

People who like this

1 Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Nov 01, 2021 at 04:09 PM 0
Share

Given the error message, I doubt AddComponent is responsible for the error.

However, I suspect you're calling DestroyImmediate(go.GetComponent<XXX>()) instead.

avatar image filkata123 · Nov 01, 2021 at 04:47 PM 0
Share

@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.

avatar image Hellium filkata123 · Nov 01, 2021 at 06:40 PM 0
Share

Does go already have any kind of renderer? (MeshRenderer / SkinnedMeshRenderer / ...)

avatar image filkata123 Hellium · Nov 01, 2021 at 07:16 PM 0
Share

Yes, but I have tried doing

 Destroy(go.GetComponent<MeshRenderer>())

before the AddComponentand I have the same result, with or without it.

It doesn't have a SkinnedMeshRenderer already, but it does have it after the AddComponent

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image filkata123 · Nov 02, 2021 at 11:23 PM 1
Share

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!

avatar image Bunny83 filkata123 · Nov 02, 2021 at 11:36 PM 0
Share

It's actually an interesting edge case I haven't come across in all those years ^^.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

321 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i import a blender armature and mesh into unity 3.5? 0 Answers

Why does adding cloth component to skinned meshrenderer move the bounds away from the mesh? 2 Answers

Importing .blend file - Missing Animation 1 Answer

Is it possible to add an animation to a mesh with no animations? 1 Answer

Modifying SkinnedMeshRenderer.sharedMesh.uv Doesnt Reset after Stopping game. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges