Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Destroying components immediately is not permitted...

Discussion in 'Scripting' started by uonlyliveonce, Sep 27, 2020.

  1. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    41
    I upgraded to Unity 2020.1.6f1 from 2019 and I'm getting this error message in the console:
     Destroying components immediately is not permitted during physics trigger/contact, animation event callbacks, rendering callbacks or OnValidate. You must use Destroy instead.


    I have a floating damage text object in my game. The text object is using TextMeshPro. In the prefab of the damage text object there is no Canvas Renderer on the text object (only a Mesh Renderer).

    However, when I run the game and the damage text is created, I get this (non-error) message:
    Removing unnecessary CanvasRenderer component from text object.


    This is immediately followed by the above error message.
    The prefab does not have a CanvasRenderer - it seems to be something that's created at runtime?

    So correct me if I'm wrong but it seems like Unity is adding a component at runtime, then immediately deleting it because it's unnecessary, then throwing an error because it's not permitted to delete it?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Have you tried updating all the packages to latest, in this case TextMeshPro?
     
    uonlyliveonce likes this.
  3. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    41
    Yes, all packages I have are fully updated.

    When I got the error for the first time I was confused because I never remove components from gameObjects (the most I do is disable them). Is there a way to stop Unity from destroying "unnecessary" components? I don't think I put unnecessary components on my gameObjects anyway.
     
    Last edited: Sep 27, 2020
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    If I had to guess, I think perhaps the TMPro Text item you are using has a "Requires CanvasRenderer" attribute on it, and something about Unity has changed so that this is not possible, but the require-force-make generates it, then it gets force-destroyed immediately??

    I will say this: if you can make a tiny project in that version of Unity, make a script / scene that trivially shows off this bug, it is DEFINITELY worth filing with Unity, because they're really good about fixing stuff like this.
     
    uonlyliveonce likes this.
  5. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    41
    I'm not sure why the CanvasRenderer component is being added - it's a TMP-Text object and not a TMPUGUI-Text object. I don't have any custom scripts on the text object (it's parent does but it doesn't have the attribute) just an Animator so if the attribute is there it's on the TMP script itself.

    Anyway - I may be the only one that ever runs into this issue so I'll just leave it be since I found a usable workaround.

    To anyone reading this that ran into the same issue - I fixed it by adding a single frame delay to the creation of the floating text with a coroutine (I don't think such a small delay will make a difference in how my game plays). The error was occuring due to the "not permitted during physics trigger/contact" part - the floating text is created on collision between an attack and an enemy.

    I think Unity could fix this issue by using Destroy() to remove unnecessary components instead of DestroyImmediate() but there might be a reason that they're using DestroyImmediate() (like not triggering the component's OnDestroy method?).

    Thanks for your help Kurt-Dekker :)