- Home /
Editor script: Changes made to another script are ignored in Play Mode
This is driving me nuts. It's probably an issue with how I have implemented the serialization or something but I am still trying to figure out where exactly the problem lies.
I have a script attached to a gameobject which uses a couple of standalone scripts to create a series of waypoints for the camera to traverse through. All the waypoints are stored in a list in one of these classes. The classes and the variables are all marked as serialized/serializable. There is an editor script which is used to create and modify the waypoints. It creates a reference to the main script via the gameobject in the hierarchy on initialization and works from there.
Now here is the problem:
Adding new waypoints works perfectly, no problem with the usual editor mode/play mode switches. But when I delete or update one of the waypoint entries in the list, the play mode completely ignores the update and keeps using the entries from before the update. For example, Debug shows that the deletion happened and the count of elements in the list went down by one, but when the play mode is triggered, the count is again shown as the original value and the applications happily goes through the elements as if there was no update or deletion done.
I compared the add function to the delete and update function but can't see why one works and the others don't. Here are the functions in question:
public Waypoint AddWaypoint(Vector3 position, Quaternion rotation, float transitionTime = 2.0f, float delay = 1.0f)
{
// create a new waypoint object
Waypoint waypoint = new Waypoint {
Position = position,
Rotation = rotation,
TransitionTime = transitionTime,
TransitionDelay = delay
};
// add it to the list
m_Waypoints.Add(waypoint);
// return waypoint object
return waypoint;
}
public void RemoveWaypoint(Waypoint waypoint)
{
//m_Waypoints.Remove(waypoint);
int index = m_Waypoints.FindIndex(x => x.Equals(waypoint));
Debug.Log("WaypointManager :: RemoveWaypoint :: index: " + index);
if (index > -1)
{
Debug.Log("Waypoint found! Deleting now. Count: " + m_Waypoints.Count);
m_Waypoints.Remove(m_Waypoints[index]);
//EditorUtility.SetDirty(m_Waypoints);
Debug.Log("Waypoint deleted. Count: " + m_Waypoints.Count);
}
}
public Waypoint UpdateWaypoint(Waypoint waypoint, Vector3 position, Quaternion rotation, float transitionTime = 2.0f, float delay = 1.0f)
{
int index = m_Waypoints.FindIndex(x => x.Equals(waypoint));
Debug.Log("WaypointManager :: UpdateWaypoint :: index: " + index + ", count: " + m_Waypoints.Count);
if (index > -1)
{
Waypoint target = m_Waypoints[index];
target.Position = position;
target.Rotation = rotation;
target.TransitionTime = transitionTime;
target.TransitionDelay = delay;
// ugh
//m_Waypoints[index] = target;
return target;
}
return null;
}
Any ideas what could be going wrong here?
Edit:: Another thing I have noticed is that if I remove or update the elements right after adding them then both delete and update functions work perfectly. But if play it once and then try to do the same thing then the play mode ignores those changes in all the subsequent play session.
Thanks in advance.
Are you saying that you're changing the object in edit mode, and then when you press 'play' it loses those changes?
Yes. So, adding a new waypoint means adding a waypoint object to the list (m_Waypoints), which is working perfectly. But the removal and update of existing elements in that same list is being ignored the moment I switch to Play mode.
Edit:: Another thing I have noticed is that if I remove or update the elements right after adding them then both delete and update functions work perfectly. But if play it once and then try to do the same thing then the play mode ignores those changes in all the subsequent play session.