property transferring problem

I am setting a property for my character in the main menu, but for some reason the property I set doesn’t get transferred to my game scene. I’m unsure as to why this is happening. I have a variable transferring it. and it isn’t null when I check it in the hierarchy until it moves to the game scene. Any thoughts?

It sounds like you have two easy solutions.

  1. Call DontDestroyOnLoad() on the object to make sure that it isn’t destroyed when the new scene is loaded. Then your character in the game scene can reference the values from your man menu scene.
  2. Use the PlayerPrefs class to store an int or a string. Then read the value when you load the game scene.

Which one you use isn’t that big a deal. DontDestroyOnLoad() might be a little more versatile for a case like this where you are just passing data from one scene to the next, but PlayerPrefs are saved so that you can access the same information even if the application is closed then reopened.

If you’re transferring an int or string, it really doesn’t matter much which one you use. If you have any other class, then you need to pass it through a reference in a script on a GameObject that isn’t being destroyed. Otherwise you would have to do serialization and that probably isn’t worth it just to take some data from the main menu to level 1.