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 aad7615 · Jan 15, 2020 at 07:19 PM · editorvariablesdeleteundoondestroy

Need help reassigning variables after the deletion of an object has been undone.

~ Problem Summary ~
I'm having trouble with Undo functions and trying to reassign variables after the deletion of an object has been undone. The objects that are attached to the object I've deleted lose their variable references once the object deletion is undone. Meanwhile the Parent Constraints are not adjusted back to the original constraints.
~ Background ~
All of my code is meant to run in the editor, not during play mode. The simplest way I've been testing this has been with three game objects:
alt text - L1 has none for the startLink and L2 for the endLink. L1 has no Parent Constraint.
- L2 has L1 for the startLink and L3 for the endLink. L2 has L1 as a Parent Constraint.
- L3 has L2 for the startLink and none for the endLink. L3 has L2 as a Parent Constraint.
- I delete L2 using the Delete Key, my script runs and adjusts the values for both L1 and L3.
- L1 now has L3 as its endLink and L3 has L1 as its startLink. L3 moves to L2s old position and now has L1 as a Parent Constraint.
- I undo L2's deletion using CTRL + Z and the L2 Object reappears with its variables and Parent Constraint matching what it was previously.
- L1 and L3 now have both their startLinks and endLinks set to none. L3 does not move and still has L1 as a Parent Constraint.
~ Script ~

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Animations;
 #if UNITY_EDITOR
 using UnityEditor;
 using UnityEditor.SceneManagement;
 using UnityEngine.SceneManagement;
 #endif
 
 [SelectionBase]
 [ExecuteAlways]
 public class ChainLink : MonoBehaviour {
 
     public LinkPiece link;
     public ChainLink startLink, endLink;
 
     #if UNITY_EDITOR
 
     void OnDestroy () {
 
         // Stops from running when destroying objects on scene load/switch
         if(Time.frameCount == 0){
             return;
         }
 
         //Same as above along with preventing this from running in Playmode
         if (EditorApplication.isPlayingOrWillChangePlaymode || (!EditorApplication.isPlayingOrWillChangePlaymode && EditorApplication.isPlaying)) {
             return;
         }
 
         // Update the start link
         if (startLink) {
             PrefabUtility.RecordPrefabInstancePropertyModifications (startLink.gameObject);
             Undo.RegisterCompleteObjectUndo(startLink, "Save Start Link Values");
             startLink.endLink = endLink;
         }
 
         // Handle new end link
         if (endLink) {
             PrefabUtility.RecordPrefabInstancePropertyModifications (endLink.gameObject);
             Undo.RegisterCompleteObjectUndo(endLink, "Save End Link Values");
             // Assign the new link
             endLink.startLink = startLink;
 
             // Remove itself as the endlink's source
             endLink.GetComponent<ParentConstraint> ().RemoveSource (0);
 
             // Adjust Positions Quickly
             AdjustLinkPositions (endLink);
 
             // Create a new source using the startlink
             ConstraintSource startSource = new ConstraintSource ();
 
             if (startLink) {
                 PrefabUtility.RecordPrefabInstancePropertyModifications (startLink.gameObject);
                 Undo.RegisterCompleteObjectUndo(startLink, "Save Start Link Values");
                 startSource.sourceTransform = startLink.link.pivotObject;
                 startSource.weight = 1;
             }
 
             endLink.GetComponent<ParentConstraint>().AddSource (startSource);
         }
     }
 
     public void AdjustLinkPositions (ChainLink chainLink) {
         //move the positions of the links to their propper locations
         if (!chainLink.startLink) {
             return;
         }
         PrefabUtility.RecordPrefabInstancePropertyModifications (chainLink.gameObject);
         Undo.RegisterCompleteObjectUndo(chainLink, "Update track positions");
         chainLink.transform.position = chainLink.startLink.link.pivotObject.position;
         chainLink.transform.rotation = chainLink.startLink.link.pivotObject.rotation;
         Physics.SyncTransforms ();
 
         // loop through if there are additional links
         if (chainLink.endLink) {
             AdjustLinkPositions (chainLink.endLink);
         }
     }
 
     #endif
 }
 
 [System.Serializable]
 public class LinkPiece {
     public Transform mainObject, pivotObject;
 }

~ End ~
I am trying to get the links to go back to the "Normal" state after the deletion of the middle link has been undone. I would appreciate any help you can provide.

linkexample.png (34.8 kB)
Comment
Add comment · Show 1
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 aad7615 · Jan 21, 2020 at 11:08 PM 0
Share

Bumping this to ask if I should change the topics I've tagged or if there's anything I can do to make my question clearer. Unsure if there's a better way to store the changes rather thank using the Undo class.

0 Replies

· Add your reply
  • Sort: 

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

156 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 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

Detect when GameObject has been deleted / removed from scene 10 Answers

OnDestroy() callback in Editor upon deleting selected GameObject - del key 5 Answers

Constant error : SaveSnapshot called without previous call to MakeSnapshot 1 Answer

MonoBehaviour references are lost on Undo 0 Answers

How to Edit variables of Scripts in A list? 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