Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share GameObjects between scenes

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.

like image 916
Fabian Bigler Avatar asked Sep 26 '22 04:09

Fabian Bigler


1 Answers

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....
}
like image 150
Frohlich Avatar answered Oct 11 '22 10:10

Frohlich