5

Let's assume I have a GameObject 'Player' and two scenes A and B. If I add this GameObject 'Player' on both scenes and then make some changes (e.g. adding a script in Scene A), can I somehow achieve that the GameObject 'Player' stays the same in both A and B? Or do I have to update the GameObject in both scenes manually?

I couldn't find a convenient way to achieve this.

Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70
  • Sounds like you're looking for [prefabs](http://docs.unity3d.com/Manual/Prefabs.html). – rutter Nov 18 '15 at 18:50
  • 1
    Why you would put this GameObjet into A and B instead of transfer the same from A to B when you change the scene? The most common approach thinking in player (your own example) is to create it at level 0 or some savepoint and persist it as singleton throught levels until it dies, so the question is "why you need to avoid this common approach and place 2 players that are the same think into each level?" – Frohlich Nov 18 '15 at 18:54
  • @rutter I'm looking for a way to share the gameobjects between scenes, not the scene itself. Can prefabs achieve that too? – Fabian Bigler Nov 18 '15 at 18:56
  • @Frohlich How would you do that? – Fabian Bigler Nov 18 '15 at 18:57
  • DontDestroyOnLoad() method tells Unity to persist a GameObject between scenes... You just need to declare it on one of your behaviour's Awake/Start state... – Frohlich Nov 18 '15 at 18:59
  • @Fabian Bigler I thought you may want to do something different than just persist object between scenes, that's why I ask before answer ;) If your concern is just about persistence between scenes so DontDestroyOnLoad() will solve it =) – Frohlich Nov 18 '15 at 19:08

1 Answers1

5

If you just need to persist GameObjects between scene transitions you can use DontDestroyOnLoad() method.

Something like this should makes de deal:

using UnityEngine;
using System.Collections;

public class MyPlayer : MonoBehaviour {
    void Awake() {
        DontDestroyOnLoad(this.gameObject);
    }

    // myPlayer behaviour....
}
Frohlich
  • 953
  • 7
  • 17