Don't Destroy Object? and Global Class?

Scene Change -> Alive Object. i don’t know…

a.scene
aaa.node - always object
bbb.node - normal object

change scene

b.scene
aaa.node - alive

==============================

c#-unity
GlobalScript.cs

class GlobalScript
{
public static GlobalScript Instance;
public int aaa = 0;
void Awake()
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
}

MyScript.cs

start()
{
GlobalScript.Instacne.aaa = 1;
}

this script -> cocos creator ???

What kind of Object is it?
Why not just destroy and recreate?

Do you know the “singleton pattern”? You could save your nodes in there and reuse them (retain | release)?
Or a better way: save the state of the Nodes in a singleton class and recreate them with this state data.

I know singleton.

Does Cocos creator keep a singleton even if it changes scene?

Yes, why not?
You can create a singleton class in the Loading Scene use it as you wish and later release it in the GameOver Scene.

If you remove a node from parent and you want to reuse it, you have to use node.retain().
If you do not need the node anymore use node.release()

keep in mind that You are responsible for releasing all nodes in the singleton class and of course the singleton itself. Also have an eye on the memory usage.

test scene
image

Singleton.js

var Singleton = cc.Class({
extends: cc.Component,

properties: {
    Label : cc.Label,
    MyNode: cc.Node
},

statics: {
    instance: null
},

onLoad: function () {
    Singleton.instance = this;
},

});

aaa1.js

var Singleton = require(“Singleton”);

cc.Class({
extends: cc.Component,

properties: {
    btn : cc.Button,
},

onLoad () {
    Singleton.instance.MyNode = this.node;
},

start () {
    this.btn.node.on('click', this.MyClick, this);
    Singleton.instance.Label.string = "-----";      <-- error
},

MyClick : function(_btn)
{
    _btn.node.getChildByName("Label").getComponent(cc.Label).string += "1";
    cc.director.loadScene("Main");
}

});

Main Scene
image

ddd1.js

var Singleton = require(“Singleton”);
cc.Class({
extends: cc.Component,

properties: {
    lblTest:
    {
        default:null,
        type: cc.Label,
    }
},

start () {
    Singleton.instance.Label.string = "-----";
},

});

error
SCRIPT5007: SCRIPT5007: Unable to set property ‘string’ of undefined or null reference
SCRIPT5007: Unable to set property ‘string’ of undefined or null reference

Main Scene2

image

ddd1.js

var Singleton = require(“Singleton”);
cc.Class({
extends: cc.Component,

properties: {
    lblTest:
    {
        default:null,
        type: cc.Label,
    }
},

start () {
    // Singleton.instance.Label.string = "-----";
    this.lblTest.string = Singleton.instance.Label.string;
},

});

This does not keep the singleton.

I tried to see the basic sample.
Do you have any other samples?

Okay, I thought you want to use the Singleton in your Game not in the Editor.

Check the CC-Docs: global variable

Thank you. I found. cc.game.addPersistRootNode