Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Help from professional game developers!

A topic by Hi-Tech Developerz created Sep 06, 2022 Views: 238 Replies: 3
Viewing posts 1 to 2

Hello, I am a solo indie game developer who is stuck in a trouble. I want to save a player’s name and want to use it in another scene. But as the new scene loads (in unity), the name data is gone!

Kindly help me with my problem. I want a way to store data and things even when the game isn’t running. Also, I want it to be like I save it in one scene and use it in any of the scenes. Your comments will be appreciated!

(1 edit)

There are many, many ways you can do this depending on the complexity you need, but first you should know why that data isn't persisting currently.  It sounds like you are assigning the player's name to some MonoBehaviour attached to a GameObject in the scene.  You can do that, but (by default) when you unload a scene, every object in the scene is destroyed and its data is lost.  Even if you have e.g. a copy of the same prefab in every scene, each one is still a separate object with its own state.

A simple way to do this would be to create a ScriptableObject containing variables for the player's name and whatever other persistent data you need (health / ammo / etc) and set the values there.  Then anywhere you reference that ScriptableObject in the application, you will see the same values, because there is only ever one copy of a ScriptableObject and it is not tied to a scene.  To get the data to persist after closing the game, you would have to write those values to a file when the player saves the game, then read them from the file when the player loads the game.

Hey, thanks for your help! It worked and gave me a way to do other stuff like ammo, life tracking, reloading, etc. I really appreciate your help.

Although, If you don’t mind, I have been now stuck to another bug. You see that while using instantiate for patrol enemies, only one of them follows the player. The other enemies just move around until the enemy which is following the player is destroyed. Is there a way to go around this hustle?

How did you implement this?

This topic has been auto-archived and can no longer be posted in because there haven't been any posts in a while.