I’m a complete novice with Unity and I know almost nothing about coding, but I’ve been following tutorials and ran into an issue where the player would suddenly not be in the location he’s supposed to be in the scene view after hitting Play.
I narrowed the cause down to this script, and besides having no idea what I’m even looking at, I couldn’t begin to guess what/why it’s happening. If someone could explain it, that’d be much appreciated.
I mean this code loads save data, and then uses the position in the save data to position the player. That’s why the player is being moved.
But this is loading save data seemingly from one scene and loading the player’s position from a different scene, and I don’t know how to stop it from doing that.
There’s nothing in the code that checks the scene before positioning the player. That functionally would need to be added on your end.
You can of course just disable the component while testing.
You could change the name of the file depending on the scene. So rather than using a somewhat hardcoded string (initialised in Start for some reason), add a method to generates a path for the scene as needed:
private string GetSceneSavePath()
{
string sceneName = this.gameObject.scene.name;
return Path.Combine(Application.persistentDataPath, $"{sceneName}Data.json");
}
This only makes sense if the data is per-level. For save data encompassing the players overall progress, you’ll need to introduce a system for tracking data on a per-level basis.
Though if you don’t understand the code you’re looking at, I think your time is better spent doing some guided learning on C# and Unity (rather than random tutorials) to get your bearings: Junior Programmer Pathway - Unity Learn
Appreciate your time. I think you’re right about learning the basics. I can kinda gleam the idea of what the code is supposed to be doing by looking at it, but the syntax is what I don’t understand. I’ve tried so many times and even attempted an undergrad degree, but I just bounce off it every time. It’s like I’m not wired in a way that I can make sense of this stuff.