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! :cry:

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 :expressionless:

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 *= 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 = mySlave.csCameras[(i+1)];_
mySlave.csCameras*.name = “Mgp_Cam”+i.ToString();
_ }
else*
mySlave.csCameras = 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 = mySlave.csCameras[(i+1)];
mySlave.csCameras*.name = “Mgp_Cam”+i.ToString();*
* }
else*

mySlave.csCameras = 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:
[41891-serializedproblem0.png|41891]
[41892-serializedproblem1.png*|41892]*
*
*

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