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 /
This question was closed Mar 08, 2019 at 04:35 PM by VRAI_Chris for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by VRAI_Chris · Mar 08, 2019 at 03:57 PM · editoreditor-scriptingcomponentundo

How to undo removing components in an editor extension?

Hi. I'm trying to create an editor extension which will remove a component from multiple objects & replace it with another. It's all working fairly well, but I want to be able to undo the changes in the editor.

Undoing the addition of the new component is straightforward enough, Undo.AddComponent() does the job. However, there's no Undo.RemoveComponent(), & no other function seems to work. Undo.RecordObject() & Undo.RecordObjects() don't seem to register anything. Undo.RegisterCompletedObjectUndo() does add an event to the undo stack, but when I call it, the components don't reappear & I get an error message for each object saying "Component could not be loaded when loading game object. Cleaning up!".

 //Undo.RecordObjects(objects, "Upgrade Text");
                     
 for (int i = allTextObjects.Count - 1; i >= 0; i--)
 {
     Text text = allTextObjects[i];
     
     GameObject target = text.gameObject;
     Undo.RegisterCompleteObjectUndo(target, "Remove Text");
     
     //Record text parameters
     DestroyImmediate(text);
     
     TextMeshProUGUI newtext = Undo.AddComponent<TextMeshProUGUI>(target);
     //Assign text parameters to textmeshpro
 }

Am I missing something in the Undo documentation, or is there some other way to achieve this?

Comment
Add comment · Show 5
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 troien · Mar 08, 2019 at 04:05 PM 0
Share

I think you want to use Undo.DestroyObjectImmediate. Didn't test it though, but I guess you should use that ins$$anonymous$$d of Object.DestroyImmediate (which is probably what you are using atm)...

avatar image VRAI_Chris troien · Mar 08, 2019 at 04:13 PM 0
Share

No, either of those would destroy the entire object, but I only want to destroy a component attached to the object.

avatar image troien VRAI_Chris · Mar 08, 2019 at 04:27 PM 1
Share

No, they don't :p. They accept Object as parameter, $$anonymous$$onoBehaviour inherrits Object. If you pass a GameObject as a parameter, it destroys the gameobject, if you pass a $$anonymous$$onoBehaviour as a parameter it destroys only the $$anonymous$$onobehaviour (i.e. removes the monobehaviour from the gameobject)...

Same as with Destroy

How do you remove the component currently? I assumed with DestroyImmediate because there is no such function as 'RemoveComponent' as far as I'm aware...

Show more comments
avatar image RobAnthem · Mar 08, 2019 at 04:29 PM 0
Share

In the docs, it specifically says this

Important: To correctly handle instances where objectToUndo is an instance of a Prefab, PrefabUtility.RecordPrefabInstanceProperty$$anonymous$$odifications must be called after RecordObject.

So basically this means you cannot use Undo.RecordObject by itself, you need to also call`PrefabUtility.RecordPrefabInstanceProperty$$anonymous$$odifications`

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Event for loading component in the editor 2 Answers

Undo.RecordObject isn't working. 7 Answers

How to properly handle Undo events in custom inspector? 0 Answers

How to add a component on a GameObject in Custom Inspector 1 Answer

Undo SetSiblingIndex for root objects? 1 Answer


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