- Home /
What is EditorUtility.CopySerialized supposed to do?
If I, for example, use this function with the parameters of
- the default Game Object in a Unity Scene, the "Main Camera"
and
- 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?
Answer by Jaap Kreijkamp · Dec 04, 2009 at 01:09 AM
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);
}
}
Answer by Jessy · Dec 11, 2009 at 03:37 AM
(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?
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.
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.