Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Using Destroy on GameObject components not working?

Discussion in 'Scripting' started by mdallicardillo, Apr 8, 2021.

  1. mdallicardillo

    mdallicardillo

    Joined:
    Apr 8, 2021
    Posts:
    4
    Hello all, thanks for taking a look at this. This is my first attempt at game development so pardon my lack of knowledge.

    Does anyone have any idea why the following code is doing nothing?

    player is a reference to the main player script, and animationManager, collisionManager, etc are references to monobehaviour component scripts on the player GameObject, stored in the main player script.

    Code (CSharp):
    1.         public override void UnSocket( PlayerMain player )
    2.         {
    3.             Destroy( player.animationManager );
    4.             Destroy( player.collisionManager );
    5.             Destroy( player.inputManager );
    6.             Destroy( player.movementController );
    7.         }
    I read that using Destroy() on a game objects component would remove the component, but running this code does nothing. After I attempt to remove these components I add new ones using AddComponent, which works fine, but the ones I attempted to Destroy are also still there.

    Thanks in advance, hopefully someone has some insight!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,516
    Destroy works fine. Are you sure you have references to the correct components? Are you maybe referencing components on a prefab for example?

    Also note that Destroy does not finish destroying the object until the end of the frame, so you will still see those components for the remainder of the current frame.
     
    Joe-Censored likes this.
  3. mdallicardillo

    mdallicardillo

    Joined:
    Apr 8, 2021
    Posts:
    4
    Hmm, the player object is instantiated from a prefab, but the prefab only has the PlayerMain script component, none of the others, and when I run the game and check the player clone on the hierarchy window, that's where I see the multiple script components adding up, the prefab doesn't change. But you're right, I know destroy works, I use it for full GameObjects in other places, so the only thing that makes sense is that the references I'm giving it are not pointing where I think they are.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,516
    You can always do this:
    Code (CSharp):
    1. Debug.Log("Player animation manager", player.animationManager.gameObject);
    Then if you click on that log message the editor should highlight the exact object that it's on, whether that's in the scene or in your project folder.
     
  5. mdallicardillo

    mdallicardillo

    Joined:
    Apr 8, 2021
    Posts:
    4
    Wow, amazing tip thank you! The game object it found is the Player clone in the scene hierarchy, not the prefab it was created from in the project folders. So in theory destroy should be acting on the correct components or the correct game object. Perhaps it doesn't like to Destroy script components? I'm going to try to just remove the rigidbody component and see if that works in the same block of code.
     
  6. mdallicardillo

    mdallicardillo

    Joined:
    Apr 8, 2021
    Posts:
    4
    So that worked fine:


    Destroy( player.gameObject.GetComponent<Rigidbody2D>() );


    Does correctly remove the rigidbody from the player clone. However, the nearly identical line:


    Destroy( player.gameObject.GetComponent<DefaultMoveController>() );


    Does NOT remove the DefaultMoveController script component from the clone. Not sure what could be going on unless removing scripts isn't allowed, or perhaps the custom scripts need to handle the OnDestroy() event? At a bit of a loss here as to why I can't remove the script components...
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,516
    Is there any chance you have code that adds these components _back_ to the object? Like maybe checking in update if the component exists and adding it if it doesn't?
     
unityunity