You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot报错:Invalid set index 'texture'(基于null实例)求助

Godot报错解决:Invalid set index 'texture' (on base: 'null instance')

报错原因

错误核心是你尝试给空实例设置texture属性,问题出在第二个挂载于TextureRect的脚本中。

问题代码分析

第二个脚本开头是extends TextureRect,说明该脚本直接绑定在TextureRect节点上。但你用$TextureRect是在当前节点下查找名为TextureRect的子节点——而当前节点本身就是TextureRect,不存在这个子节点,所以$TextureRect返回null,赋值操作触发报错。

修改方案

直接去掉$TextureRect前缀,使用节点自身的texture属性即可:

extends TextureRect

func _ready():
    if randi() % 2 == 0:
        texture = load("res://Blue Jeans.png")
    else:
        texture = load("res://Brown Boots.png")

额外说明

如果你的Item.tscn根节点就是这个修改后的TextureRect,第一个脚本创建实例并添加子节点的逻辑无需改动,修改第二个脚本后即可解决报错。

内容的提问来源于stack exchange,提问作者Tyler Bert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:18:28