Hello everyone,
I’m developing a sort of acrade-ish 4 player game and currently I’m having trouble with transfering certain data from one scene to another.
In my main level scene I have a MatchController script that takes care of everything that has to happen within a match. It also contains certain data like the amount of players, player names, player scores, match time and other important values. Once the match ends, the game is supposed to switch to a another scene where the results of the last match are shown. But in order to do that, I have to somehow get all that data from the previous scene into the current one.
I have experimented with a few solutions to this problem, however, I don’t know what is the best and most efficient way to do it.
Using DontDestroyOnLoad:
public class MatchController : MonoBehaviour
{
private void EndMatch()
{
DontDestroyOnLoad(gameObject);
SceneManager.LoadScene("MatchEndScreen");
}
}
Using DontDestroyOnLoad(), the MatchController would survive the scene swap and, using the code I found in the first answer here [<- Link], could be accessed again in the match results scene using the sacraficial lamb technique. However, I feel like all of that is pretty wonky. Moving the entire MatchController through scenes and then having to sacrafice another game object just to retrieve it… it works but… there must be a better way.
Using a static class:
public class MatchController : MonoBehaviour
{
private int[] scores = new int[4];
private void EndMatch()
{
MatchResults.scores = scores;
SceneManager.LoadScene("MatchEndScreen");
}
}
public static class MatchResults
{
public static string[] playerNames { get; set; } = new string[4];
public static int[] scores { get; set; } = new int[4];
}
Using a static class, I have a place I can simply copy all the important values of the MatchController to once the match ends. And once the results scene has finished loaded, a central script somewhere in that scene can simply read those values again and process them. One issue though is that a static class is being instantiated on game launch, I believe. Meaning that even in scenes where the match results are completely irrelevant, like the main menu for example, this static class will reserve memory that might be needed somewhere else. I mean… it is only a small class with a few variables. But stuff like that could add up with time and perhaps other game modes or minigames.
Other ideas:
Here are some other ideas that I has but didn’t try out yet. Just feel like I should mention the possibility. Another solution could be saving all the important values in a file, like a JSON or XML file that could then be retrieved in the match results scene. But perhaps it creates other problems like the implementations of a file reader/writer and stuff.
And then I somewhere read that you can load a new scene with parameters but I didn’t look too much into it. Maybe it’s a super simple solution I just haven’t tried out yet. Lol.
So yeah, those were my thoughts on how transfering data from one scene to another could be done. Hoping to hear some input from you guys on what’s the smartest idea to handle this. Or maybe I’m just being a die hard perfectionist and the static method is fine as it is, lol.