import { _decorator, Component, Node } from 'cc';
import { B } from './B';
const { ccclass, property } = _decorator;
@ccclass('A')
export class A extends Component {
start()
{
let tmp = B.instance.cClass.myNumber;
console.log(tmp);
}
}
import { _decorator, Component, Node } from 'cc';
import { C } from './C';
const { ccclass, property } = _decorator;
@ccclass('B')
export class B extends Component {
private static _instance: B;
public static get instance () {
if (this._instance) {
return this._instance;
}
this._instance = new B();
return this._instance;
}
@property({type : C})
public cClass : C = null;
}
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('C')
export class C extends Component {
public myNumber = 10;
}
I have A class, B singleton class, C class
A class will call B class via singleton instance, then call myNumber in C class.
Running the A class with output this error ’ [PreviewInEditor] Cannot read property ‘myNumber’ of null’
I make sure that I have drag the C class into B singleton class in editor. I think i my understand something wrong about how singleton works in Typescript.