Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by M-G-Production · Mar 05, 2015 at 06:15 PM · c#editorvariableserializationproblem during runtime

Serialize problem with Unity Editor, values reset on game execution!

Hi folks!

I recently started to write some Editor scripts and this is awesome! I can clearly see the full power of Unity! But... When I execute my game, my variables come back to initial values! :'(

So I looked for System.Serializable and what it does. I though I found the solution, but still no changes...

There is no need to look at ALL of my scripts, there is no syntax errors or no errors Unity warn me about... I really think the problem is about serialization :|

Thanks for your help! (<3)


So here is my "Normal" script:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 //EVEN WHEN I SERIALIZE
 [System.Serializable]
 public enum ChangeOn
 {
     timer = 0,
     animationEnd = 1,
     tapIt = 2,
     dontChange = 3
 }
 
 //EVEN IF I DO IT TWICE... IT WONT WORK...
 [System.Serializable]
 public class mgp_cutScene : MonoBehaviour 
 {
     public static ChangeOn changeOn;
     public bool changing = false;
 
     public Object mgpCam;
     public Object[] cutScenes;
     public int currentScene = 1;
 
     public int camNumber = 0;
     public GameObject[] csCameras;
 
     public Object theCam0, theCam1, theCam2, theCam3;
     public float theCam0Timer, theCam1Timer, theCam2Timer, theCam3Timer, chrono;
     public bool theCam0Active, theCam1Active, theCam2Active, theCam3Active;
     
     void OnEnable()
     {
         Debug.Log ("CutScene Activated!");
     }
 
     public void AddCamera()
     {
         //This function adds a camera in the scene
         csCameras = new GameObject[camNumber+1];
         csCameras[camNumber] = (GameObject)Instantiate (mgpCam, Vector3.zero, new Quaternion(0,0,0,0));
         csCameras[camNumber].name = "Mgp_Cam"+camNumber.ToString ();
         csCameras[camNumber].transform.parent = gameObject.transform;
 
         //Rebuild the csCamera Array
         for (int i = 0; i < camNumber; i++)
         {csCameras[i] = GameObject.Find ("Mgp_Cam"+i.ToString());}
 
         //camNumber += 1;
         GameObject[] camsQt = GameObject.FindGameObjectsWithTag ("MgpCam");
         camNumber = (camsQt.GetLength(0));
     }
 
     public void DestroyAll ()
     {
         //This fonction destroy all MGP cams
         foreach (GameObject camera in GameObject.FindGameObjectsWithTag("MgpCam"))
         {DestroyImmediate(camera);}
 
         //Initialize the array (it looks like it reset it?)
         csCameras.Initialize();
         camNumber = 0;
     }
 
     // Update is called once per frame
     void Update ()
     {
         //This calculate the time since the activation
         chrono = transform.FindChild ("CS_Trigger").GetComponent<mgp_cutSceneTrigger> ().chrono;
     }
 }



Here is my Editor Script

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomEditor (typeof (mgp_cutScene))]
 
 //So I tried to serialize the EDITOR script too!
 [System.Serializable]
 public class mgp_cutSceneEditor : Editor {
 
     Vector2 scrollPosition;
     float timerMin = 0.1f;
     float timerMax = 300;
 
     // Use this for initialization
     public override void OnInspectorGUI()
     {
         //mySlave refer to the mgp_cutScene Class
         mgp_cutScene mySlave = (mgp_cutScene)target;
 
         //This is the prefab you need to drag from MGP_CutScene/Prefabs
         EditorGUILayout.LabelField("Drag Mgp_Cam prefab from MGP - CutScene", EditorStyles.boldLabel);
         mySlave.mgpCam = (Object)EditorGUILayout.ObjectField("Mgp_Cam Prefab: ",mySlave.mgpCam,typeof(Object));
 
         //Always check the Cameras number
         int cameras = (mySlave.camNumber);
 
 
         /******************************************************************************************************/
         //If there is only one camera
 
 
         if (cameras >= 1)
         {
             //TITLE
             EditorGUILayout.LabelField("First Camera Settings", EditorStyles.whiteBoldLabel);
 
             //CAMERA VALUES
             mySlave.theCam0 = mySlave.csCameras [0];
             mySlave.theCam0 = (Object)EditorGUILayout.ObjectField("First Camera",mySlave.theCam0, typeof(Object));
             //Is it running?
             mySlave.theCam0Active = EditorGUILayout.Toggle("Is Active", mySlave.theCam0Active);
             //Change on a certain time or a certain event?
             mgp_cutScene.changeOn = (ChangeOn)EditorGUILayout.EnumPopup("Change Camera on: ", mgp_cutScene.changeOn);
 
             switch (mgp_cutScene.changeOn)
             {
             case ChangeOn.timer:
                 EditorGUILayout.LabelField("Please set the time needed to change Cam", EditorStyles.boldLabel);
                 mySlave.theCam0Timer = EditorGUILayout.Slider("Cam Timer (Sec): ", mySlave.theCam0Timer,timerMin,timerMax);
                 break;
 
             case ChangeOn.tapIt:
                 EditorGUILayout.LabelField("Tap it to set time from activation", EditorStyles.boldLabel);
                 //System that calculates the time from the activation of the Trigger
                 if (Application.isPlaying)
                 {
                     if (mySlave.chrono > 0)
                     {
                         EditorGUILayout.FloatField("Chrono from activation: ",mySlave.chrono);
 
                         if (GUILayout.Button("Tap-It Now!"))
                         {
                             mySlave.theCam0Timer = mySlave.chrono;
                             Debug.Log ("Will change on "+mySlave.chrono.ToString()+" seconds from cutscene activation!");
                         }
                     }
                 }
                 else
                 {
                     EditorGUILayout.HelpBox("Use on Game Execution only!", MessageType.Warning);
                 }
                 EditorGUILayout.Space();
                 break;
 
             case ChangeOn.animationEnd:
                 //System that wait till the camera end their translations
                 EditorGUILayout.LabelField("When the cam finish animation",  EditorStyles.boldLabel);
                 mySlave.changing = EditorGUILayout.Toggle ("Activated: ", mySlave.changing);
                 break;
 
             case ChangeOn.dontChange:
                 EditorGUILayout.LabelField("Please set the timer to end the cutscene",  EditorStyles.boldLabel);
                 mySlave.theCam0Timer = EditorGUILayout.Slider("Cam Timer (Sec): ", mySlave.theCam0Timer,timerMin,timerMax);
                 break;
             }
 
             if (GUILayout.Button ("Delete First Camera"))
             {
                 if (cameras == 1)
                 {
                     mySlave.DestroyAll();
                 }
                 else
                 {
                     DestroyImmediate(mySlave.theCam0);
                     //mySlave.theCam0 = null;
                     for (int i = 0; i < (mySlave.csCameras.GetLength(0)-1); i++)
                     {
                         if (mySlave.csCameras[i+1])
                         {
                             mySlave.csCameras[i] = mySlave.csCameras[(i+1)];
                             mySlave.csCameras[i].name = "Mgp_Cam"+i.ToString();
                         }
                         else
                             mySlave.csCameras[i] = null;
                     }
                     mySlave.camNumber -= 1;
                 }
             }
 
             EditorGUILayout.Space();
         }
 
 
         /******************************************************************************************************/
         //If there is 2 cameras
 
 
         if (cameras >= 2)
         {
             //TITLE
             EditorGUILayout.LabelField("Second Camera Settings", EditorStyles.whiteBoldLabel);
             
             //CAMERA VALUES
             mySlave.theCam1 = mySlave.csCameras [1];
             mySlave.theCam1 = (Object)EditorGUILayout.ObjectField("First Camera",mySlave.theCam1, typeof(Object));
             //Is it running?
             mySlave.theCam1Active = EditorGUILayout.Toggle("Is Active", mySlave.theCam1Active);
             //Change on a certain time or a certain event?
             mgp_cutScene.changeOn = (ChangeOn)EditorGUILayout.EnumPopup("Change Camera on: ", mgp_cutScene.changeOn);
             
             switch (mgp_cutScene.changeOn)
             {
             case ChangeOn.timer:
                 EditorGUILayout.LabelField("Please set the time needed to change Cam", EditorStyles.boldLabel);
                 mySlave.theCam1Timer = EditorGUILayout.Slider("Cam Timer (Sec): ", mySlave.theCam1Timer,timerMin,timerMax);
                 break;
                 
             case ChangeOn.tapIt:
                 EditorGUILayout.LabelField("Tap it to set time from activation", EditorStyles.boldLabel);
                 //System that calculates the time from the activation of the Trigger
                 if (Application.isPlaying)
                 {
                     if (GUILayout.Button("Tap-It Now!"))
                     {
                         //tapTimer = 
                         Debug.Log ("Will change on X seconds from cutscene activation!");
                     }
                 }
                 else
                 {
                     EditorGUILayout.HelpBox("Use on Game Execution only!", MessageType.Warning);
                 }
                 EditorGUILayout.Space();
                 break;
                 
             case ChangeOn.animationEnd:
                 //System that wait till the camera end their translations
                 EditorGUILayout.LabelField("When the cam finish animation",  EditorStyles.boldLabel);
                 mySlave.changing = EditorGUILayout.Toggle ("Activated: ", mySlave.changing);
                 break;
                 
             case ChangeOn.dontChange:
                 EditorGUILayout.LabelField("Please set the timer to end the cutscene",  EditorStyles.boldLabel);
                 mySlave.theCam0Timer = EditorGUILayout.Slider("Cam Timer (Sec): ", mySlave.theCam0Timer,timerMin,timerMax);
                 break;
             }
             
             if (GUILayout.Button ("Delete First Camera"))
             {
                 if (cameras == 1)
                 {
                     mySlave.DestroyAll();
                 }
                 else
                 {
                     DestroyImmediate(mySlave.theCam0);
                     //mySlave.theCam0 = null;
                     for (int i = 0; i < (mySlave.csCameras.GetLength(0)-1); i++)
                     {
                         if (mySlave.csCameras[i+1])
                         {
                             mySlave.csCameras[i] = mySlave.csCameras[(i+1)];
                             mySlave.csCameras[i].name = "Mgp_Cam"+i.ToString();
                         }
                         else
                             mySlave.csCameras[i] = null;
                     }
                     mySlave.camNumber -= 1;
                 }
             }
             
             EditorGUILayout.Space();
         }
 
 
         /******************************************************************************************************/
         //IF THERE IS 3 CAMERAS
 
 
         if (cameras >= 3)
         {
             EditorGUILayout.LabelField("Third Camera Settings", EditorStyles.whiteBoldLabel);
             mySlave.theCam2 = mySlave.csCameras [2];
             mySlave.theCam2 = (Object)EditorGUILayout.ObjectField("Third Camera",mySlave.theCam2, typeof(Object));
             mySlave.theCam2Timer = EditorGUILayout.Slider("Cam Timer (sec): ", mySlave.theCam2Timer,timerMin,timerMax);
         }
 
 
         /******************************************************************************************************/
         //IF YOU HAVE 4 CAMERAS
 
 
         if (cameras >= 4)
         {
             EditorGUILayout.LabelField("Last Camera Settings", EditorStyles.whiteBoldLabel);
             mySlave.theCam3 = mySlave.csCameras [3];
             mySlave.theCam3 = (Object)EditorGUILayout.ObjectField("Last Camera",mySlave.theCam3, typeof(Object));
             mySlave.theCam3Timer = EditorGUILayout.Slider("Cam Timer (sec): ", mySlave.theCam3Timer,timerMin,timerMax);
         }
 
         /*if (cameras > 0)
         {
             //THIS SHOWS THE ARRAY of the CutScenes
             EditorGUILayout.LabelField("This is you Cameras", EditorStyles.boldLabel);
             serializedObject.Update();
             var controller = target as mgp_cutScene;
             EditorGUIUtility.LookLikeInspector();
             SerializedProperty cSCams = serializedObject.FindProperty ("csCameras");
             EditorGUI.BeginChangeCheck();
             EditorGUILayout.PropertyField(cSCams, true);
             if(EditorGUI.EndChangeCheck())
                 serializedObject.ApplyModifiedProperties();
             EditorGUIUtility.LookLikeControls();
         }*/
 
         if (cameras <= 3 && mySlave.mgpCam)
         {
             if (GUILayout.Button("Add a Camera"))
             {
                 mySlave.AddCamera();
             }
         }
 
         if (GUILayout.Button("Reset!"))
         {
             mySlave.DestroyAll();
         }
     }
 }
 



When I press "Play" button, this is what it does: alt text

alt text

serializedproblem0.png (141.1 kB)
serializedproblem1.png (132.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Mar 05, 2015 at 09:57 PM

Try adding EditorUtility.SetDirty(ComponentYouAreAccessing); after making a change in your editor extension.

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 M-G-Production · Mar 06, 2015 at 06:08 PM 0
Share

Hey Cherno, thanks for your help! What is the point of this line of code? Where should I put it in my Editor script? The component I am accessing? I'm accessing my mgp_cutScene with mgp_cutSceneEditor... I'm a bit confused

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

21 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Cannot serialize System.Type field 3 Answers

Unity Editor - Class variable value contrain 4 Answers

Distribute terrain in zones 3 Answers

Copy reference from one component to another derived component., 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