Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by user-12539 (google) · May 10, 2011 at 08:34 PM · editorcomponentreplaceextend

Replace Existing Component?

So I have a Monobehaviour class (Item), and a class that inherits from it (WearableItem).

Similarly to how the Editor asks whether you would like to replace a collider with a different collider, I'd like to have only one Item script on a GameObject at any time. If I were to put another on it, it would prompt me to replace the existing one, just like Unity does with colliders.

I don't have much experience extending the editor thusfar, but what should look toward to get started?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by L Elf · Oct 23, 2013 at 03:49 PM

This code works without additional components:

 public class Test : MonoBehaviour {
     void Reset() {
         if (GetComponents<Test>().Length > 1) {
             Invoke("DestroyThis", 0);
         }
     }

     void DestroyThis() {
         DestroyImmediate(this);
     }
 }
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 rostok · Jan 08, 2018 at 12:55 AM 0
Share

Shouldn't the Invoke be executed from found components? For example like

 GetComponent<Animated$$anonymous$$esh>().Invoke("DestroyThis", 0);
avatar image
1

Answer by Bunny83 · May 10, 2011 at 10:11 PM

Well, first i thought it wouldn't be possible because there's no direct event that tells you that a Component has been added. But i've figured out that Reset() is called in the editor to initialize new components.
I thought well now it's easy but unfortunately Reset is called right before the adding-process is completed. Calling DestroyImmediate inside Reset() will crash Unity :D.

You can't use the normal Destroy in the editor so that's the only way to remove the second component. In the end I've found a solution. Not as easy as i thought but also not that difficult:

DeleteComponent.cs is a helper script that is executed in editmode. We need that just for delaying the DestroyImmediate call. Just place it somewhere in your project. You don't need to attach this to something, that's done via script. It just destroys another component and itself when start is invoked (which happens right after it has been attached).

// DeleteComponent.cs using UnityEngine; using System.Collections;

[ExecuteInEditMode()] public class DeleteComponent : MonoBehaviour { public Component componentReference = null; void Start () { if (componentReference != null) DestroyImmediate(componentReference); DestroyImmediate(this); } }

Here's the actual script:

using UnityEngine; using System.Collections;

public class WearableItem : Item { #if UNITY_EDITOR void Reset () { Component OldItem = GetComponent<Item>(); if (OldItem != null && OldItem != this) { if (UnityEditor.EditorUtility.DisplayDialog("Component already exists", "Do you want to replace it?", "Ok, replace it", "No, thanks!")) { gameObject.AddComponent<DeleteComponent>().componentReference = OldItem; } else { gameObject.AddComponent<DeleteComponent>().componentReference = this; } } } #endif }

I've adjusted the classnames for your case. Maybe you want to check against Item instead of WearableItem.

Reset is only called in the editor (not at runtime) but since I use something from the editor namespace i placed the whole Reset() function in preprocessor tags to make sure it doesn't go into the build.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to replace the Button component in the editor with a subclassed version 1 Answer

Extend Unity ObjectField / Array function/Gui interface 1 Answer

Is it possible to override SceneView / AnimX Private Types? 0 Answers

Multi-Object Editing not supported 1 Answer

Custom [Require*] attribute 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