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
0
Question by magnusm · Sep 22, 2013 at 10:10 AM · javascriptsceneundoregister

Understanding The Undo Function (JavaScript)

Heyo!

I am trying to use the undo function in Unity, but can't get it to work. I have been looking at the ScriptReference but there's a lot of superfluous info making it really difficult for me to understand.

How would I go about performing the most basic Undo?
Where one function registers the entire scene, saving it how it was. Then another function performs the Undo, reverting the scene back to that saved state.

Whenever I try to use Undo.RegisterSceneUndo(), I get the error message: 'RegisterSceneUndo' is not a member of 'Undo' I am clearly doing something wrong?

Cheers for the help, Magnus :)

Comment
Add comment · Show 4
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 AdenFlorian · Sep 22, 2013 at 10:28 AM 0
Share

I think you mean JavaScript. Java is an entirely different language, not that anyone would misunderstand you here, since there are only 3 possible languages you could be talking about. Also, this problem isn't necessarily a language specific problem, I think, just pointing it out.

Are you using the latest version of Unity? Sometimes people use older versions of Unity, but read from the up to date script reference on the website. Some functions and classes were not there from the beginning and were added sometime along the way.

What version of Unity are you using?

avatar image AdenFlorian · Sep 22, 2013 at 10:40 AM 0
Share

Is the error inside monodevelop? (or whatever IDE you use)

Also, please paste some of the code that includes the call to Undo.RegisterSceneUndo()

avatar image AdenFlorian · Sep 22, 2013 at 11:01 AM 0
Share

Undo.RegisterSceneUndo() seems to need a string parameter.

Undo.RegisterSceneUndo("Name of undo save")

avatar image AdenFlorian · Sep 22, 2013 at 11:07 AM 0
Share

This is in a script that inherits from Editor, right?

http://docs.unity3d.com/Documentation/ScriptReference/Editor.html

2 Replies

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

Answer by Bunny83 · Sep 22, 2013 at 11:25 AM

The Undo class is an editor class and can only be used inside the Unity editor. Usually there's a note on the doc page but it seems they forgot it. However if you search the clas in the left class-list you will find the Undo class in the editor-classes-list.

In short you can't use this class in your game.

Comment
Add comment · Show 3 · 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 magnusm · Sep 22, 2013 at 11:34 AM 0
Share

Oh right, I get ya! So its a similar deal to pressing Ctrl Z in the Editor?

$$anonymous$$an that's so annoying. Is there any way of perfor$$anonymous$$g an Undo function in Runtime then?

avatar image Bunny83 · Sep 22, 2013 at 11:44 AM 0
Share

Unity doesn't provide such a functionality at runtime. You either save the previous state yourself or might want to use the UnitySerializer.

Since it's your game you know best what can be changed by the user and what you want to be revertable. This might be the position / rotation / scale of certain objects or your some custom variables of your own scripts.

avatar image magnusm · Sep 22, 2013 at 11:56 AM 0
Share

UnitySerializer looks like it will do the trick. I will give that a go, thanks :)

avatar image
1

Answer by kasplask · Oct 31, 2013 at 01:24 AM

UnitySerializer is great, i really applaud whydoidoit for his efforts, but it does introduce some (at least to me) unwanted noise. Another option, something that works out great for me, is to make a clone of the object that you're performing an action on, storing its transform values, making it a child of a GameObject that you've hidden out of the way and then disabling it. You can then undo the action of the object by destroying the current object and replacing it with the clone.

```javascript // A custom action class public class Action { var originalObject : GameObject; var storedObject : GameObject; var storedPosition : Vector3; var storedRotation : Vector3; var storedScale : Vector3;

 function Action ( o : GameObject ) {
     originalObject = o;
     storedObject = MonoBehaviour.Instantiate ( originalObject );
     storedObject.name = storedObject.name.Replace("(Clone)","");
     
     storedPosition = originalObject.transform.position;
     storedRotation = originalObject.transform.localEulerAngles;
     storedScale = originalObject.transform.localScale;        
 }

}

// Needed vars public var actions : List.< Action > = new List.< Action >(); public var currentAction : Action; public var undoContainer : Transform;

// Add an action to the actions list public function AddAction ( obj : GameObject ) { var newAction : Action = new Action ( obj );

 newAction.storedObject.transform.parent = undoContainer;
 newAction.storedObject.transform.localPosition = Vector3.zero;
 newAction.storedObject.SetActive ( false );
 
 actions.Add ( newAction );

     // Set the maximum amount of undo actions
 if ( actions.Count > 10 ) {
     actions.RemoveAt ( 0 );
 }
 
 currentAction = newAction;

}

// Undo the selected action public function UndoAction ( action : Action ) { action.storedObject.transform.parent = currentLevel.transform; action.storedObject.transform.position = action.storedPosition; action.storedObject.transform.localEulerAngles = action.storedRotation; action.storedObject.transform.localScale = action.storedScale; action.storedObject.SetActive ( true );

 if ( action.originalObject ) {
     Destroy ( action.originalObject );
 }
 
 actions.Remove ( action );
 
 if ( actions.Count > 0 ) {
     currentAction = actions[actions.Count-1];
 } else {
     currentAction = null;
 }

}

// Undo the current action public function UndoCurrentAction () { if ( currentAction != null ) { UndoAction ( currentAction ); };
} ```

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

18 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

Related Questions

Multiple Cars not working 1 Answer

How to have animation play correctly 1 Answer

Cant Find The Variable In Another Script? 1 Answer

Help with Kongregate API? 0 Answers

How can i make my scene less laggy? 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