Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    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. }