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 chufraise · Mar 04, 2014 at 05:41 PM · editor-scriptingserializationeditorwindow

Serializing a Transform instance in a custom inspector window

Hi all! Im trying to create a custom inspector for level generation. I have run into some troubles tho.

I want to serialize a Transform object so it will persist between Play sessions and also when Unity has quit.

I made a test case for you to look at that will illustrate my problem:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class MyWindow : EditorWindow {
 
     [SerializeField]
     private Props props;
 
     [MenuItem("Test/Test")]
     public static void ShowWindow() {
         EditorWindow.GetWindow(typeof(MyWindow),false,"Test");
     }
 
     void OnEnable() {
         if(props == null) {
             props = new Props();
         }
     }
 
     void OnGUI() {
         props.OnGUI();
     }
 }
 
 [System.Serializable]
 public class Props {
 
     [SerializeField]
     public Transform parent;
 
     public void OnGUI() {
         parent = EditorGUILayout.ObjectField("Parent Transform",parent,typeof(Transform),true) as Transform;
     }
 }


I've created a window which shows a field to which you can drag an object from the hierarchy panel. All works fine but when restart Unity or press Play and Stop, the field is "None" and the reference is lost.

I have tried to follow the best practices on this, but clearly something has gone wrong.

Anyone know what the problem is?

Thanks!

Comment
Add comment · Show 2
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 chufraise · Mar 05, 2014 at 08:04 AM 0
Share

Bumpeli bump!

avatar image chufraise · Mar 05, 2014 at 03:42 PM 0
Share

Anyone???

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MakeCodeNow · Mar 05, 2014 at 07:17 PM

A Transform member var is a reference to the transform, not the transform itself. If you want to have the Transform serialzied, make a prefab out of it. If you don't want to make a prefab, you'll need to manually extract properties from the transform, save them in individual Vector3/Quat/etc properties, and then create a new Transform/GameObject on start.

Comment
Add comment · Show 1 · 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 chufraise · Mar 05, 2014 at 10:08 PM 0
Share

Thanks for answering! Ok, it makes sense that Unity doesn't serialize references to Transform objects. But I'm not sure what you mean by making a prefab? I have to, some how, serialize the instance of the prefab. Along with all its children.

The level generator works like this: You drag a parent transform to the custom inspector window. Then you hit a GENERATE button and a level is built by instanciating prefabs of various kind and added to the hierarchy using the the mentioned parent transform as parent. All the children are stored in a List. But since the parent transform isn't serialized then neither is the list of children.

avatar image
0

Answer by Aledend · Apr 11, 2020 at 07:20 PM

Old post but the first one that showed up on google. My workaround has been having the generation happen outside playmode and saving the instances in the scene. On startup the reference to each instance would have to be rebound with the likes of GameObject.Find and Transform.Find...

It's just a do once thing and my project's small.. so I can't tell wether this is viable when working with a larger amount of gameobjects.

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

22 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

Related Questions

OnSerialize event 2 Answers

Shared data between Editor Windows that is also persistent and serialized? 0 Answers

How to modify a Gradient SerializedProperty 0 Answers

SerializedProperty and PropertyField NullReference 1 Answer

Custom Editor window loses values on play. 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