Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HHameline · Sep 25, 2011 at 02:14 PM · editordestroyget component

Script inheiritence and finding a certain component

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Sep 25, 2011 at 03:00 PM

Of course it does. If ClassB is derived from ClassA it IS also a ClassA, that's the point of inheritance. I guess you want to know whether the object is a derived form of ClassA or an instance of the base class (ClassA) itself.

One way that came to my mind is this one (not tested):

     foreach (GameObject go in Selection.GetFiltered(typeof(GameObject), SelectionMode.DeepAssets))
     {
         ClassA tmp = go.GetComponent<ClassA>();
         if (tmp != null && tmp.GetType() == typeof(ClassA))
         {
             Editor.DestroyImmediate(tmp, true);
             go.AddComponent<ClassB>();
         }
     }

Usually GetType() should return the true type of the instance. In the case of a ClassB instance the comparison should fail. If that doesn't work you can also test the component against ClassB and only perform your actions when it's not a ClassB.

 if (tmp != null && !(tmp is ClassB))

However, this is not as versatile as the first way, since it just test against ClassB. The first way should work for any ClassA derived classes.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HHameline · Sep 25, 2011 at 03:11 PM 0
Share

Thank you very much Bunny, that works great I really appreciate you taking the time to help. Hans

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do I break apart an object? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Who owns NetworkViews placed in the editor? 1 Answer

OnTriggerEnter - destroy (this.gameobject) if it collides with anything 2 Answers

How can I preserve static object/data between editor and play? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges