Code after load prefab not being executed...

hi,
I want to know whether loading a prefab and instantiating takes a lot of time and how to perform actions on the prefabs in that case…here is the code :

        cc.loader.loadRes("Category", function (err, prefab) {
            self.catNode = cc.instantiate(prefab);
            self.node.addChild(self.catNode);
        });

            this.catNode.position.y = this.yTop;
            this.catScript = this.catNode.getComponent("GameCategoryStorage");

            this.catScript.txt = this.gameCatJSON["title"];

I have a prefab Category in the resources folder. in the prefab I have a tree structure of nodes where the root node contains a typescript file called gameCategoryStorage that contains a class GameCategoryStorage . I am getting errors where I try to set the y position of the catNode, and am also getting an error when I assign a string value from JSON to catScript.txt where txt is a string field in the class.
could someone tell me what I am doing wrong??? I think the prefab is taking time to load and that is why I can’t perform actions on the instantiated prefab soon after the loaders code. if so how do I get around it? for example how do I add a delay inside the code if that’s the solution???

thanks!!!

I would move the last three lines into the loadRes function and change this.* to self.*

resource loading is async, so the code will continue to run WHEN loading resource. If you need to do something AFTER async action, put the codes to the callback of the async function, so it will run AFTER the async action finished

hi @MikeFromMars , @smellypotato ,
I put the later code in the callback function but I think the getComponent function also is async and when I assign a value to a property of the class, it is still trying to assign the script to the catScript variable…hence I am getting the following error…

Uncaught TypeError: Cannot read property ‘txt’ of null

can you please tell me how I can assign (or read) a property of the script after getComponent(“class name”) ???

thanks!

getComponent is not async. However, if you use “this” inside a function defined inside another function, “this” no longer refers to the script you are in.
Method 1: add .bind(this) to bind the script to the function so you can use this as normal
cc.loader.loadRes(“Category”, function() {
this.catNode = cc.instantiate(prefab);
}.bind(this));

Method 2: create a local variable before loadRes and assign this to it, then you can replace “this” with the variable you just created
let _this = this;
cc.loader.loadRes(“Category”, function(err, prefab) {
_this.catNode = cc.instantiate(prefab);
});

show us your code if you still get any errors

Hi,
two things…

  1. using loadres before instantiating a prefab is not necessary. just create a property of type cc.Prefab and instantiate that. that happens instantly and there is no mismatch with the following code.
  2. in getComponent(), we have to pass the name of the script file minus the extension. I was passing the class name which was different from the file name.
    hope this is of help.
    Thanks!