0 votes

I don't understand why I getting this error, the node is there but the editor keeps telling the node instance is null. It would be helpful if someone could point out where the problems is, thank you.

ERROR: Invalid set index 'texture' (on base: 'null instance') with
value of type 'StreamTexture'

Scene:
Door (Area2D)
- Sprite

Script:

extends Area2D

onready var sprite = $Sprite
export var type = 0 setget set_texture

func set_texture(newSprite):
    type = newSprite
    if type == 1:
        sprite.texture = load("res://Image/Door01.png") 
    if type == 2:
        sprite.texture = load("res://Image/Door02.png") 
Godot version v3.2.3
in Engine by (244 points)

1 Answer

0 votes
Best answer

It depends on where you're calling set_texture() like for instance if you had that function in _enter_tree() or _init() before the node even enters the sceneTree can cause that.

Also onready var sprite = $Sprite states that Sprite is a direct child of Door

by (4,480 points)
selected by

Thanks for the comment, as you mention it is likely caused by the node has been called before ready. I have added a line of if is_instance_valid(sprite):in the settexture function, and it works.

As for the node relation, Sprite is a direct child of Door. Is it better to use Sprite as parent node and Area2D as a child node? Thank you.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.