I have two scenes.
The first is a menu and the second there are function based on selected model in Menu.
I have some scripts that are Static scripts used only in the second scene.
When I am backing from the second scene to Menu the data in static variables keep saved in memory, of course, it is static.
But I dont use it in Menu and I need reset this data when I load a new model in second scene.
How is the best way to reset this data? One variable by one? Or is possible to “destroy” a static script?
public static class LPressController
{
public static LPressList selectedLPress = new();
...
This code is just a sample. In my code I have:
if(selectedLPress != null)...
When I load the second scene this selectedLPress need to be null.
I am using Statics because this scripts are used by others in the second scene.
You can’t destroy anything in C#. It’s a garbage collected language.
The best answer would be to not use statics. Or at the very least, use a singleton instance so that the data is still per-instances with a global accessor.
Though when closing the scene, clearing the data is the most simple solution. Encapsulating said relevant data into a single object so that you can just null out in one go would be a good way to consolidate this.
Kurt would say something along the lines of “there have been rivers of digital ink written on the subject”, and he would be right. You don’t need to tell me the pros and cons of singletons; it is probably one of the mostly debated and discussed programming patterns. I feel like you should know this by now as well.
In fact,
To infer from the code you posted,
The questions and answers you need are
It seems more appropriate to “what method should be used for the purpose”
rather than “proper initialization method of static variables”.
Perhaps the reason you used static is because it is easily accessible from any location.
As I think, if what you’re trying to do is work related to input/control,
it would be good to refer to examples of New Input System or the structure of Rewired
Study the concept of input control management structure that separates the layer of controller and action
or
If you really need something data that needs to be managed separately,
I think it would be better to manage the life of the dependent object that needs that data when it is created/destroyed.
Singletons is “pattern”.
It can be implemented in a static way, or it can be used in a different way.
What exactly is a singletons
What exactly is static
I recommend taking this opportunity to learn more about it