Error calling method from instanciated scene

Godot Version

4.2.2

Question

Two extremely simple scripts and one problem

FIRST SCRIPT (class_name Projectile)
just add print_something() method

extends RigidBody3D
class_name Projectile

func _ready():
	pass

func _process(delta):
	pass

func print_something(): #THIS METHOD I WAN TO CALL
        print("print_something")

SECOND SCRIPT
instantiae Projectile scene and call print_something()

extends StaticBody3D
@export var projectile_count = 4 
@export var radius = 2
@onready var projectile_scene = load("res://scenes/environment/projectile.tscn")

func _ready():	
	pass
func _process(delta):
	pass

func _on_area_body_entered(body):
	
	for i in range(projectile_count):
		
		var instance = projectile_scene.instantiate()
		var projectile_instance = instance as Projectile
		
		instance.print_something() 
        #Invalid call. Nonexistent function 'print_something' in base 'RigidBody3D'.

		projectile_instance.print_something() 
        # Invalid call. Nonexistent function 'print_something' in base 'Nil'.
		
	pass

why i can’t call print_something() method after instantiation? i didn’t get it

Error for Instance

#Invalid call. Nonexistent function ‘print_something’ in base ‘RigidBody3D’

Error for Instance as Projectile

#Invalid call. Nonexistent function ‘print_something’ in base ‘Nil’

I’m guessing here that the “_on_area_body_entered(…)” method is called before the staticbody3d has been added to the tree because that’s when @onready is called and initialize variables, etc.

The giveaway is the error message:

Nonexistent function ‘print_something’ in base ‘Nil’

→ In base ‘Nil’

thx you for respond. i found the problem. i just messed to attach script to projectile scene. its deattach by itself for some reason. a lot of hours finding out the problem, lol

It’s ok with that:

var instance = projectile_scene.instantiate() as Projectile
instance.print_something()

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.