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

Question How to delete component?

Discussion in 'Immediate Mode GUI (IMGUI)' started by AzpxMsw, Nov 6, 2022.

  1. AzpxMsw

    AzpxMsw

    Joined:
    Jun 1, 2020
    Posts:
    13
    I need to delete one component on selected object using a script. Here's an example:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class SampleWindow : EditorWindow
    7. {
    8.     Vector2 scrollView = Vector2.zero;
    9.  
    10.     [MenuItem("Window/Others/SampleWindow #W", false, 0)]
    11.     static void OpenBSCMTools()
    12.     {
    13.         EditorWindow.GetWindow(typeof(SampleWindow));
    14.     }
    15.  
    16.     void OnGUI()
    17.     {
    18.         scrollView = EditorGUILayout.BeginScrollView(scrollView);
    19.         EditorGUILayout.BeginHorizontal();
    20.         if (GUILayout.Button("Delete Box Collider", GUILayout.MinWidth(115), GUILayout.MaxWidth(135)))
    21.         {
    22.             if (Selection.activeGameObject != null)
    23.             {
    24.                 //Here I need to delete BoxCollider using a button
    25.             }
    26.         }
    27.         if (GUILayout.Button("Delete Mesh Collider", GUILayout.MinWidth(115), GUILayout.MaxWidth(135)))
    28.         {
    29.             if (Selection.activeGameObject != null)
    30.             {
    31.                 //Here I need to delete MeshCollider using a button
    32.             }
    33.         }
    34.         EditorGUILayout.EndHorizontal();
    35.         EditorGUILayout.EndScrollView();
    36.     }
    37. }