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.

6
  • Sounds like you're looking for prefabs.
    – rutter
    Commented Nov 18, 2015 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
    Commented Nov 18, 2015 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? Commented Nov 18, 2015 at 18:56
  • @Frohlich How would you do that? Commented Nov 18, 2015 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
    Commented Nov 18, 2015 at 18:59

1 Answer 1

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....
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.