Hi Vincent,
Typically you’d add a member variable when you want to use the data it holds multiple times and/or across multiple methods.
Looking at the code you have posted for the SceneLoader.cs there is absolutely no reason to have it as a member variable at all as the only place you use it is within the one method, LoadStartScene
.
On that note, the line of code you have within that method;
FindObjectOfType<GameStatus>();
currently isn’t doing anything, well, it is, but nothing helpful. It will perform the “find” but doesn’t do anything with what it returns. Equally, the levelReset.LevelReset
line of code will generate an error if it executes because levelReset
has not been instantiated/initialised.
I suspect the reason you don’t see this happen is because before those two lines of code you use SceneManager.LoadScene(0)
which loads the other scene before these lines of code have chance to execute.
As a final suggestion, it’s often better to name your variables based on what they are, so for example, you member variable;
GameSession levelReset;
It isn’t really a “level reset”, this is just one of the behaviours that class may provide. It may be better named as;
GameSesson gameSession;
In the above, it is a GameSession. Then, when you call your LevelReset
method, your code would read like this;
GameSession.LevelReset();
which perhaps provides the reader with a clearer idea of what it is and what its doing than;
levelReset.LevelReset();
The above would be behaviour.behaviour as opposed to thing.behaviour in the previous example.
Hope the above is of some help