x


What is EditorUtility.CopySerialized supposed to do?

If I, for example, use this function with the parameters of

  1. the default Game Object in a Unity Scene, the "Main Camera"

and

  1. an empty Game Object

then "Game Object" will be renamed "Main Camera", but lose all of its components, including its Transform.

This can't be the intended use! So what is?

more ▼

asked Dec 03 '09 at 09:13 PM

Jessy gravatar image

Jessy
15.7k 72 95 196

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You need to use it on all components as well, but in my experience this results in crashing Unity. You can use it however to clone script settings to a new object. For example the script below copies the properties from JDiceSideUp script in object dice to (existing) object newDice (if script isn't there yet it will be added).

using UnityEngine;

using UnityEditor;
using System.Collections;

public class JTestEditorClass {


    [MenuItem("Ctrl-J/Set Script Properties")]
    static void TestCopy() {
    	GameObject orig = (GameObject) GameObject.Find("dice");
    	GameObject modify = (GameObject) GameObject.Find("newdice");

    	Component fromScript = orig.GetComponent(typeof(JDiceSideUp));
    	Component toScript = orig.GetComponent(typeof(JDiceSideUp));
    	if (toScript == null) {
    			toScript = go.AddComponent(fromScript.GetType());
    	}
    	EditorUtility.CopySerialized(fromScript, toScript);
    }
}
more ▼

answered Dec 04 '09 at 01:09 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left

(First off, I apologize for not putting this in the comments, but UnityAnswers provides no functionality that I can see for a lengthy response, or one that includes code.) :-P

Thanks, but there is some kind of serious problem here. Using the simpler case from above, I believe the resulting script is this:

using UnityEngine;
using UnityEditor;

public class JTestEditorClass {


[MenuItem("Ctrl-J/Set Script Properties")]
static void TestCopy()
{
    Component mainCamera = GameObject.Find("Main Camera").GetComponent( typeof(Camera) );
    EditorUtility.CopySerialized
    (
    	mainCamera, GameObject.Find("Game Object").AddComponent( mainCamera.GetType() )
    );
}


}

However, after doing that, it is impossible to remove the Camera component from the previously empty Game Object. This error also occurs:

referencedGO != this

Now what?

more ▼

answered Dec 11 '09 at 03:37 AM

Jessy gravatar image

Jessy
15.7k 72 95 196

As I said, CopySerialized seems to be buggy and crashes my environment often. It does work okay on scripts so you can use it to safely copy the script data. But other than that, I'd say CopySerialized is a bugreport worthy. As I'm plagued by so many bugs in Unity I've stopped reporting them, if project pressure drops I'll go and file a book full.

Dec 11 '09 at 05:56 AM Jaap Kreijkamp

Well, that's good enough for me. You answered what it's "supposed" to do, even if it doesn't do a very good job. Thanks again.

Dec 11 '09 at 06:36 AM Jessy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x349

asked: Dec 03 '09 at 09:13 PM

Seen: 1312 times

Last Updated: Dec 03 '09 at 09:13 PM