I’m not native speaker.
My GameObject has included multi components.
example
GameObject
|-UI.button
|-UI.Image
|–Myc#Script1
|–Myc#Script2
|–Myc#Script3
I want to remove only c# script but I dont want to put name of type like this.
Destroy(gameObject.GetComponent<"MyC#Script1">());
Destroy(gameObject.GetComponent<"MyC#Script2">());
Destroy(gameObject.GetComponent<"MyC#Script3">());
but I have try to make them dynamic ,then look my code
foreach (MonoBehaviour script in obj.GetComponents<MonoBehaviour>()) {
//if(script == isC#Script)
//{ Destroy(script)}
Debug.Log(script.GetType()+ " : "+script.GetType().IsClass);
//IsClass always tell me " True" +_+ How to separate them
}
Thank you.
Well, most if not all Unity components are also just C# scripts, so the fact that you code always returns true is not really strange. The fastest thing you can do is check the namespace, as all Unity components are either in the ‘UnityEngine’ or the ‘UnityEditor’ namespace:
foreach (MonoBehaviour script in obj.GetComponents<MonoBehaviour>())
{
var namespaceName = script.GetType().Namespace;
if(namespaceName == null ||
(!namespaceName.Contains("UnityEngine") &&
!namespaceName.Contains("UnityEditor")))
{
Destroy(script);
}
}
Other option are to give all your custom scripts a specific Interface, Attribute or base class, but then you will have problems if you want to also remove potential third party scripts.
Thank you for your advice sir.
May I have one question. Could you tell me if I add multiple script at a GameObject is this the righ way to code ?