0 votes

This function complains: Invalid set index 'text' (on base: 'Nil') with a value of type 'String'. I don't understand why.

extends KinematicBody2D

onready var registration = $Label

func set_ship_registration(text):
    registration.text = text

when setshipregistration(text) is called in Main:

func generate_ship(scene, x, y, reg):
    var ship = scene.instance()
    ship.position = Vector2(x, y)
    ship.set_ship_registration(reg)
    add_child(ship)

There's a sample project here: https://github.com/imekon/text1

asked Oct 13, 2018 in Engine by imekon (229 points)

1 Answer

+2 votes
Best answer

Hi,

You need to parent your ship before you can access it, so you just need to move your add_child(ship) so that it comes after you instantiate your ship.

answered Oct 13, 2018 by nucky9 (50 points)
selected Oct 13, 2018 by imekon

@nucky9's answer is correct. and @imekon you need to know what the onready keyword does.
http://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html?highlight=onready#onready-keyword

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.