How can I make objects transfer between scenes?

I’m making a 2D side scroller game but I’m having trouble with a couple of things. So I have houses in the background where the player can go into them and explore, and each house is a different scene. The problem is to spawn the player I’m using

(Instantiate (player, Vector3.zero, Quaternion.identity) as GameObject

in my code to spawn my player, which is handy when it comes to respawning after deaths, but I can’t figure out how to make the player transfer over between scenes, since the script I used that code in is in a different scene than the house scene.

Any suggestions would be helpful.

2 Answers

2

DontDestroyOnLoad(gameObject);

DontDestroyOnLoad Is often used in Awake()

Try and use a somewhat singleton design when working with multiple scenes

C# Example:

public class DDOL : MonoBehaviour {

	public static DDOL instance; 

	// Use this for initialization
	void Awake () {
		if (instance == null) {
			DontDestroyOnLoad (gameObject);
			instance = this;
		} 
		else if (instance != this) {
			Destroy(gameObject);
		}
	}
}