jlak
September 18, 2024, 1:41pm
1
Godot Version
4.3 stable
Question
https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/instancing_starter.zip
extends Node
@export var Ball: PackedScene
func _input(event):
if event.is_action_pressed("click"):
var new_ball = Ball.instantiate()
new_ball.position = get_viewport().get_mouse_position()
add_child(new_ball)
With this demo i can run and all thing works
If i click a new ball is created , but if in script I change the variable name of Ball, click can not work any more as expected
But not work even I change it back
Quick tip: you can format your code in your posts by selecting all the code and pressing ctrl+e. It makes it much easier to troubleshoot.
Do you get an error message in the debugger?
jlak
September 18, 2024, 2:21pm
3
thank you for tip , I got “Cannot call method ‘instantiate’ on a null value.”
to reproduce ,
import demo repository
run, all things works
change the main script to (change only variable name)
extends Node
@export var Ball2: PackedScene
func _input(event):
if event.is_action_pressed("click"):
var new_ball = Ball2.instantiate()
new_ball.position = get_viewport().get_mouse_position()
add_child(new_ball)
run again, get “Cannot call method ‘instantiate’ on a null value.”
change code back to default
extends Node
@export var Ball: PackedScene
func _input(event):
if event.is_action_pressed("click"):
var new_ball = Ball.instantiate()
new_ball.position = get_viewport().get_mouse_position()
add_child(new_ball)
run , still get error “Cannot call method ‘instantiate’ on a null value.”
tested with MacOS 15 m2 and x86 vmware win10
You will have to place the ball scene in inspector after changing its variable name.
select your scene having this script and in the inspector load the ball scene in your ball2 var
It should look like this
2 Likes
jlak
September 18, 2024, 2:55pm
5
Thank you , I successfully make it working as expected.
I was too curious
1 Like
system
Closed
October 18, 2024, 2:55pm
6
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.