Hello, I have a class ClassB which inherits from ClassA, I’m trying to make a editor script that finds all ClassA components and removes them, and then add ClassB to them. The only problem is my script is using a .GetComponent() to see if it should remove a script and add ClassB but the GetComponent is also bringing back anything that has ClassB on it as well.
Here is the code I’m using for the editor script.
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
class FindClassA
{
[MenuItem("Custom/FindClassA")]
static void Execute()
{
foreach (GameObject go in Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets))
{
if (go.GetComponent<ClassA>() != null)
{
Editor.DestroyImmediate(go.GetComponent<ClassA>(), true);
go.AddComponent<ClassB>();
}
}
}
}
When it find a ClassA in the component it works but when it finds a ClassB it still gets in to the if and then adds a second ClassB to the game object ![]()
Thanks in advance,
Hans
Thank you very much Bunny, that works great I really appreciate you taking the time to help. Hans
– HHameline