- Home /
How to save set values of an instantiated prefab BEFORE runtime programmatically?
Hello.
I am trying to populate a two ints and a list of monobehaviour scripts for a pathfinding before runtime (from an Editor script) since I don't wanna calculate which nodes are connected in runtime (requires too much computing and will lag every time the scene loads).
public class TileNode : MonoBehaviour, IPoint
{
public int xCoord = 0;
public int yCoord = 0;
public bool isWalkable = true;
public List<TileNode> Connections = new List<TileNode>();
...
Here's the IPoint interface in case it matters:
public interface IPoint
{
int X { get; }
int Y { get; }
}
Basically in short, what I want it so do this drag and drop:
except with code instead of manually drag&dropping everywhere.
EDIT/UPDATE: Okay so I managed to populate the X and Y and the List programmatically in Edit Mode (i.e. not runtime), but when I press PLAY, the ints X and Y keep their values, but the list's values disappear. How do I make sure my programmatically added List values don't get lost on the instantiated object upon pressing PLAY?
(I guess a possible(?) solution would be to programmatically create scriptableobjects that hold the necessary information, but I wanna avoid that if possible.)
Isn't the Awake method suitable for you?, The Awake method is called when the script is loaded.
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.Awake.html
Regards
I think they want to pre-compute this in edit mode, so that running the game won't compute it at all. If so, yes, that's an Editor Script.
Sound like merely "how to properly save Editor Script variables."
Owen: Yes kind of, but maybe not quite, since the variables are set in a monobehaviour class and not in an editor class. The editor class does the setting, but it's not the class that saves the values - it merely applies the values to the monobehaviour scripts' lists.
Answer by shimauma · Apr 17, 2015 at 09:58 AM
I found a solution myself to this, so reporting it here if anyone comes across this question. The problem was that I need to call "EditorUtility.SetDirty()" on the objects that I changed (i.e. added a list value).
http://forum.unity3d.com/threads/custom-editor-losing-settings-on-play.130889/