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
2
Question by IBDelta · Jul 28, 2011 at 02:06 AM · editorcustom-inspector

Saving custom inspector references

I have a custom inspector script that references a couple of gameobjects in the scene. Unfortunately every time I close and restart unity all of the references on my custom inspector are reset to null. Is there any way of saving these references or has anyone found a workaround to do this?

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 dood_legacy · Jul 28, 2011 at 05:13 AM 0
Share

are you referring to game objects in the default references?

avatar image IBDelta · Jul 28, 2011 at 05:30 AM 0
Share

All of the references on the custom inspector script including gameobjects, int, float and bool are reset when I open unity.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by IBDelta · Aug 06, 2011 at 02:31 PM

Continuing my tradition of answering my own questions, here's the answer. The variables must be serialised. What had confused me was several forum threads that claimed serialization didn't work when unity was restarted, this is not true. To help my fellow n00b's here are two scripts that will serialise string, int, float and gameobjects. For some reason I'm getting a warning that ObjectField is no longer supported. I've used the new system of "Allow Scene Objects", implemented in the code below, but I'm still getting this warning. Is this just unity having a bit of a weird turn over the changing systems or have I done something wrong? Either way the code compiles and runs with no real problems.

The first script should be dragged onto your object in the inspector.

SerializeTest.cs

 using UnityEngine;
 
 [System.Serializable]
 
 public class SerializeTest : MonoBehaviour 
 {
     [SerializeField]
     public string TestString=" ";
     
     [SerializeField]
     public int TestInt=0;
     
     [SerializeField]
     public float TestFloat=0f;
     
     [SerializeField]
     public GameObject TestObject=null;
 }

The second script should be put into a folder called "Editor".

SerializeTestEditor.cs

 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(SerializeTest))]
 
 public class SerializeTestEditor : Editor {
     
     bool allowSceneObjects;
     
     public override void OnInspectorGUI() 
     {
         SerializeTest TargetScript = target as SerializeTest;
         
         GUILayout.Label("Serialize test");
         EditorGUILayout.Separator();
          
         GUILayout.BeginHorizontal();
             GUILayout.Label("String");
             TargetScript.TestString = EditorGUILayout.TextField(TargetScript.TestString);
         GUILayout.EndHorizontal();
         
         GUILayout.BeginHorizontal();
             GUILayout.Label("Int");
             TargetScript.TestInt = EditorGUILayout.IntField(TargetScript.TestInt);
         GUILayout.EndHorizontal();
         
         GUILayout.BeginHorizontal();
             GUILayout.Label("Float");
             TargetScript.TestFloat = EditorGUILayout.FloatField(TargetScript.TestFloat);
         GUILayout.EndHorizontal();
         
         GUILayout.BeginHorizontal();
             GUILayout.Label("Object");
             allowSceneObjects = !EditorUtility.IsPersistent(target);
             TargetScript.TestObject = EditorGUILayout.ObjectField(TargetScript.TestObject, typeof(GameObject), allowSceneObjects) as GameObject;
         GUILayout.EndHorizontal();        
     }
 }
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 eitanwass · Oct 24, 2016 at 07:06 AM 0
Share

Doesn't work any more anyway...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access Custom Inspector own gameObject 1 Answer

Drawing a custom variable in the inspector y position bugs selection 0 Answers

Custom inspector variable resets automatically 1 Answer

Custom Editor (lack of) Polymorphism 6 Answers

Custom Editor for Interface/Child classes not displaying 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