- Home /
Problem keeping List variables saved when triggering play button
I have made an Editor script (inspector) to a target script called BossScript. the BossScript have 2 vairables of type List< Obsticles>, and List< bool>. Im modifying these variables from the editor script. it all works fine till i trigger the play button and then the variables reset... (like if i had 5 objects on the list, it gets to 0). It worked fine before the last update tho, i used to do it all the time. i tried something with the Serialization but it didn't work either...
Editor script:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[CustomEditor(typeof(BossScript))]
public class BossScript_Editor : Editor
{
public override void OnInspectorGUI()
{
BossScript t = target as BossScript;
GUI.color = Color.green;
if (GUILayout.Button("Add new obsticle"))
{
t.obsticles.Add(new Obsticle());
t.showObsticles.Add(false);
}
EditorGUILayout.Space();
EditorGUILayout.Space();
GUI.color = Color.white;
for (int i = 0; i < t.obsticles.Count; i++)
{
t.showObsticles[i] = EditorGUILayout.Foldout(t.showObsticles[i], ("Obsticle " + i.ToString()));
if(t.showObsticles[i])
{
EditorGUILayout.LabelField("Properties:");
t.obsticles[i].time = EditorGUILayout.FloatField("Time:", t.obsticles[i].time);
t.obsticles[i].isCurrentObjectPosition = EditorGUILayout.Toggle("Current position?", t.obsticles[i].isCurrentObjectPosition);
if (t.obsticles[i].isCurrentObjectPosition)
{
t.obsticles[i].pos = EditorGUILayout.Vector3Field("Offset:", t.obsticles[i].pos);
}
else
{
t.obsticles[i].pos = EditorGUILayout.Vector3Field("Position:", t.obsticles[i].pos);
}
t.obsticles[i].obj = (GameObject)EditorGUILayout.ObjectField(t.obsticles[i].obj, typeof(GameObject));
GUI.color = Color.red;
if(GUILayout.Button("Delete Obsticle"))
{
t.obsticles.RemoveAt(i);
t.showObsticles.RemoveAt(i);
}
GUI.color = Color.white;
EditorGUILayout.Space();
EditorGUILayout.LabelField("Effects");
EditorGUILayout.Space();
}
}
if (GUI.changed)
{
EditorUtility.SetDirty(target);
}
}
}
BossScript.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BossScript : MonoBehaviour
{
public List<Obsticle> obsticles = new List<Obsticle>();
public List<bool> showObsticles = new List<bool>();
void Awake()
{
StartCoroutine(SpawnObsticles());
}
private IEnumerator SpawnObsticles()
{
float startTime = Time.time;
float lastInstantiationTime;
yield return new WaitForSeconds(obsticles[0].time + startTime);
lastInstantiationTime = Time.time;
for (int i = 0; i < obsticles.Count - 1; i++)
{
print(i + " TIME: " + Time.time);
yield return new WaitForSeconds(obsticles[i + 1].time - lastInstantiationTime + startTime);
lastInstantiationTime = Time.time;
}
}
}
public class Obsticle
{
public float time;
public bool isCurrentObjectPosition;
public Vector3 pos;
public GameObject obj;
}
Were they being perma-saved before? You could edit them, quit Unity, restart, and there they were again? (as opposed to only being saved within the same session.)
Then that's it. Unity wants to save after you edit, then read on Start. It was only a fluke that it sort-of worked before. At the least, you'll need to add [System.serializable]
in front of your Obstacle class (so C# knows to save it.)
Plus I'm not positive Unity will save lists -- if it isn't saving your bool List, for example. You may have to add an array. copy the list in as you Edit, and read into the List on Start.
But it worked before.. why would they change it O_o And ill check it soon
If they weren't saved when you quit the Editor, it really wasn't working. You have to close Unity sometime, and I imagine the builds always need data saved, so never worked. Even if you never upgraded Unity, you would have had to fix it, eventually.
The fact it sort of worked was a "bug in your favor." Like if a checkout guy never checks your ID for beeer. When they start carding, you can't complain about always getting underage beer before.
Answer by Kryptos · Apr 22, 2012 at 04:29 PM
You cannot save values that are not serializable. Try with this:
[System.Serializable]
public class Obstacle
{
public float time;
public bool isCurrentObjectPosition;
public Vector3 pos;
public GameObject obj;
}
And by the way, the correct spelling is Obstacle. Because obsticle means the female testicle and I don't think it is what you mean here.
LOL XDDD sorry and thank you for fixing and it works thank you ^^
Answer by Owen-Reynolds · Apr 21, 2012 at 06:04 PM
Try commenting out the two new
's in BossScript.
In my limited EditorScript experience, not in 3.5, I believe my lists were always zeroed out on running until I made code look like the standard Inspector: have BossScript only declare List<Obsticle> obsticles;
and move the "new" to the Editor script: if(t.obsticles==null) new it
. In your case, you wouldn't need that second part until you started on a fresh BossScript.
Plus, this is a good chance to practice Search and Replace (^H obsticle obstacle)
Your answer

Follow this Question
Related Questions
Add trasform to list using editor gui. 1 Answer
A node in a childnode? 1 Answer
How to add objects in Inspector 1 Answer
Why does this return a NullReferenceException? 0 Answers
Variable will not change? 1 Answer