I am trying to load a json file using the cc.loader.load function in onLoad() function as shown below, I also have used some console.log() functions to see the order of execution.

const {ccclass, property} = cc._decorator;

@ccclass export default class NewClass extends cc.Component {


objectFromResource: Object

onLoad () {
    console.log("Starting onLoad() Function")
    var url = cc.url.raw('resources/json_resource.json')
    cc.loader.load(url, (err, res) => {
        this.objectFromResource = res
        console.log("Loaded URL")
    console.log("Ending onLoad() Function")

start () {
    console.log('Starting start() Function')
    console.log(this.objectFromResource) //logging as null
    console.log('Ending start() function')

update (dt) {


The output is as following:

Cocos Creator v2.1.1
JSONReader.ts:16 Starting onLoad() Function
JSONReader.ts:33 Ending onLoad() Function
JSONReader.ts:37 Starting start() Function
JSONReader.ts:38 undefined
JSONReader.ts:39 Ending start() function
JSONReader.ts:22 {__type__: "cc.JsonAsset", _name: "json_resource", _objFlags: 0, _native: "", json: Array(784)}
JSONReader.ts:24 Loaded URL

The resource is loaded after the onLoad() and start() functions are completed. How to load the resource on OnLoad() function before the start() method is called?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.