Hi all,
I’m struggling to understand an error I’m getting in relation to loading JSON data into an object.
I’m adding the JSON as an attribute (though I don’t want to do this long term) and tried directly accessing fields
with:
var data = this.quizData.name;
this just prints the name of the file. So I tried:
var data = this.quizData.resource;
In the console this does get the contents of the JSON object but also brings up a warning ‘Cannot read property ‘resource’ of null’ in the launch script. But it clearly can’t be null if it can read the file!
This is the whole class so it’s pretty basic:
var GameBuilder = pc.createScript('gameBuilder');
GameBuilder.attributes.add('quizData', {
type: 'asset',
assetType: 'json'
});
var GameShow;
var numOfRounds;
GameBuilder.prototype.initialize = function() {
var data = this.quizData.resource;
console.log(data);
};
Is there a simpler way of accessing JSON data because this seems a bit convoluted.
launch.js:7491 TypeError: Cannot read property 'resource' of null
at script.GameBuilder.initialize (GameBuilder.js?id=10916323:21)
at Func._scriptMethod (playcanvas-stable.dbg.js:23059)
at Func._onInitialize (playcanvas-stable.dbg.js:23076)
at Func._callComponentMethod (playcanvas-stable.dbg.js:23325)
at Func._onInitialize (playcanvas-stable.dbg.js:23331)
at Function.fire (playcanvas-stable.dbg.js:608)
at Function.initialize (playcanvas-stable.dbg.js:21097)
at Application.start (playcanvas-stable.dbg.js:20645)
at launch.js:7019
at Application._preloadScripts (playcanvas-stable.dbg.js:20489)
I just need a link to a public scene that reproduces the problem. Ideally, this scene should be a simple, cut-down repro. Otherwise, it’s tough to debug.
Yeah, I’m not sure what to do at the moment. Whether to make a new project and try and re-write everything or see if I can find a way around it. Though I’m not overly confident about that as the issue seems to be in the launch script.
I was struggling with this too and it seems the problem was that I didn’t set my JSON file to ‘Preload’ in the editor. Just leaving that here for someone else to find