新人求解,代码实现动态关联预设体赋值不生效

start() {

        let node = instantiate(this.Pre);

        node.setParent(this.node);

       

        resources.load("spriteFrame", Prefab, (err, loadedPrefab)=> {

            this.node.getComponent(Script).Pre = loadedPrefab;

            console.log(this.Pre);

        });


console.log(this.node.getComponent(Script).Pre.name);

    }


预格式化文本报错[PreviewInEditor] The thing you want to instantiate is nil

编辑器没拖预制体。。image

你楼上给出答案了 楼主是要动态加载并实例化预制体 建议把this.Pre这个中间商干掉 可以封装成通用方法 传入url加载prefab然后实例化

实例化不是直接instantiate就行了吗

是直接instantiate,但至少得先有个预制体吧,楼主代码里是想直接在代码里加载预制体,结果把实例化写在加载之前,所以报错了

悟了 :grinning:

多谢大佬,确实是这样,我最开始也是先加载再实例,不过粗心把实例放在异步加载外面了。。。导致未加载完仍然报错nil,搞得我还以为不是这里的问题,现在理解了 :innocent: