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

[Solved]Component added at run time persists after destroying component and stopping editor

Discussion in 'Scripting' started by lezla, Nov 19, 2017.

  1. lezla

    lezla

    Joined:
    Nov 6, 2017
    Posts:
    2
    Hi all,



    I am creating a freekick football game. When the ball is kicked, I have a line renderer predicting the balls path (purple line in image above). I am trying to add a number of capsule colliders to points taken from the line renderer so that the collision point with the face of the net can be used by the goalkeeper as the ball interception point.

    The capsule colliders are added dynamically via script, but they persist in the editor after stopping play mode in the editor. I have tried destroying the objects during runtime, but they seem to recreate themselves on stopping play mode.

    Relevant Code:

    Code (CSharp):
    1.  
    2. private CapsuleCollider[] colliders;
    3. private GameObject football;
    4.  
    5. void Start() {
    6.  
    7.         football = GameObject.Find("Football");
    8.  
    9.         colliders = new CapsuleCollider[stepCount];
    10.      
    11.         //only adding 2 colliders as I have to manually remove from inspector
    12.         for (int i = 0; i < 2 /*colliders.Length*/; i++)
    13.         {
    14.             colliders[i]= football.AddComponent(typeof(CapsuleCollider)) as CapsuleCollider;
    15.         }
    16.  
    17. }
    18.  
    19. private void OnApplicationQuit()
    20.     {
    21.        //this code successfully destroys the colliders, tested at different time of game
    22.        //runtime but added capsule colliders always recreate themselves in the editor
    23.         var components = football.GetComponents(typeof(CapsuleCollider));
    24.         foreach (var col in components)
    25.         {
    26.             Destroy(col);
    27.         }  
    28.  
    29.         //I would prefer to use something like this though rather than completely removing
    30.         // ALL capsule colliders from the game object, but it doesn't seem to work
    31.         // foreach (var col in colliders)
    32.         //{
    33.            //Destroy(col);
    34.         // }
    35.  
    36.     }
    37.  
    This was the only thread I could find similar to my issue, but it contains no solutions:
    https://forum.unity.com/threads/add...rsist-after-destroying-the-gameobject.436376/

    Any further information needed let me know.Any generally advice on going about this problem as well would be appreciated.

    Thanks!
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I just ran this code and the capsule collider component appears and disappears after I stop play.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AddComponent : MonoBehaviour {
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         gameObject.AddComponent<CapsuleCollider>();
    12.        
    13.     }
    14.    
    15.  
    16. }
    17.  
     
    lezla likes this.
  3. lezla

    lezla

    Joined:
    Nov 6, 2017
    Posts:
    2
    Thanks for taking the time to check that fire7side, turns out I was just being an idiot.

    I had copied some of the line drawing code from elsewhere and didn't notice I had copied "[ExecuteInEditMode]" attribute at the top of the class.

    Anyways sorted now!