How to execute script only once to ensure one instance of a class?
I am loading a set of spline data from the XMLSerializer. I am using a "Spline Manager" which inherits from MonoBehaviour and is attached to my gameobject. The manager holds the "Spline" class which is a plain C# class that handles all of the load/save and the calculations for the spline. The problem is that I need to be able to load a spline in edit mode and have it persist to play mode. Currently, the spline manager creates a new instance of the spline when you play the game. This effectively loses the reference. Which means I no longer have access to the desired spline data. I have tried numerous ideas such as putting the creation of Spline in OnEnable() and other places but nothing has worked.
The question is how do I ensure only one instance of the spline class is create for each spline manager?
Example below:
public class SplineManager : MonoBehaviour
{
public Spline spline = new Spline(); // creates new instance when played...
// rest of functions to handle events
}
[XmlRoot("Spline")]
public class Spline
{
// spline data and functions
}
Your answer