- Home /
Script variables filled in editor resetting in play mode
I've encountered a problem with Unity 3.5. So far I could write script that worked in editor and manipulated the values of its members (this is List actually). And after launching play mode the List data remained. But since I've installed Unity 3.5 that trick is over. Each time list is filled by script in editor mode and I start play mode the list data is erased and the count of list members goes to 0. But if I manually fill the list by references in Inspector then it runs OK. Here is the script when the list _neighbors is filled in editor when I build the scene and set to empty each time I trigger play mode.
using UnityEngine;
using System.Collections.Generic;
[ExecuteInEditMode]
public class Positioning : MonoBehaviour
{
public List<Cell> _neighbors = new List<Cell>();
private Transform _transform;
private Vector3 _lastPos;
private CellManager _cellManager;
public List<Cell> Neighbors
{
get
{
return _neighbors;
}
set
{
_neighbors = value;
}
}
void Awake()
{
_transform = transform;
_cellManager = GameObject.Find("CellManager").GetComponent<CellManager>();
}
void Start()
{
_cellManager.AddCell(GetComponent<Cell>());
_lastPos = _transform.position;
}
void Update()
{
if (_cellManager.Cells.Count > 1 &&
Vector3.Distance(_transform.position, _lastPos) > 0.01f)
{
Cell nearestCell = _cellManager.NearestCell(_transform.position, gameObject.GetComponent<Cell>());
if (Vector3.Distance(nearestCell.transform.position, _transform.position) < 1.0f)
{
if (_neighbors.Count > 0)
{
foreach (Cell neighborCell in _neighbors)
{
if (neighborCell != gameObject.GetComponent<Cell>())
{
neighborCell.gameObject.GetComponent<Positioning>().Neighbors.Remove(GetComponent<Cell>());
}
}
_neighbors.Clear();
}
Vector3 pos =
nearestCell.GetFreeNeighborPlace(_transform.position);
_transform.position = pos;
_transform.rotation = nearestCell.transform.rotation;
List<Cell> neighbors = _cellManager.NearestCells(_transform.position, gameObject.GetComponent<Cell>());
foreach (Cell neighbor in neighbors)
{
neighbor.gameObject.GetComponent<Positioning>().Neighbors.Add(GetComponent<Cell>());
_neighbors.Add(neighbor);
}
}
}
_lastPos = _transform.position;
}
}
I know that all the public members in scripts derived by MonoBehaviour are serialized and it worked before. I have no idea what is happened. Maybe I'm doing something wrong and I should go another way. Hope someone could help me with this. Thanks.
Answer by vbbartlett · Dec 14, 2012 at 11:01 PM
This seems to have fixed it I added if (GUI.changed) EditorUtility.SetDirty(target); To the OnInspectorGUI
this solution worked for me. However, I've never encountered this problem aside from this one instance ... which makes me worry about all the other times I haven't had this problem. Also makes me wonder why this particular component / class system I'm working on requires this. Surprised to see that this solution is from 2012 as well.
Answer by Kryptos · Apr 21, 2012 at 06:38 PM
Add some Debug.Log.
I'm pretty sure the culprit is _neighbors.Clear()
. Since your script is executed in edit mode, there might be some condition that cause this part of Update
to be called once the game start.
Answer by ruben_hiet · Jan 11, 2013 at 10:32 AM
If you have a list that is containing classes you need to add [System.Serialize] on top of your class like this :
[System.Serialize] public List Neighbors { get
{ return _neighbors; } set { _neighbors = value; } }
variables are standard serializable but classes aren't even if you set public infront you need to add this on top to let your list remember its class variables. hope it helps