HI i am getting a error in my script i really need help

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By urvish07

this is the code i am very new to godot i really need help please tell me whats wrong…

extends Spatial

onready var main = get_tree().current_scene
var enemy = load(“res://enemy.gd”)

func spawn():``
var enemy = enemy.instance()
main.add_child(enemy)
enemy.transform.origin = transform.origin + Vector3(rand_range(-15,15), rand_range(-10,10)

func _on_Timer_timeout():
spawn()

I am getting a error on func _on_Timer_timeout(): line please help…

Quote the error, what does it say ?

Inces | 2022-01-15 18:15

:bust_in_silhouette: Reply From: DaddyMonster

A couple of things:

  1. You defined the variable enemy twice, once as a member variable and once as a variable in a method. That’ll throw an error for redefining a variable. My convention is to call the add scene onto the name of the scene as below.
  2. You had a Vector3 with only 2 components (I just put zero in the third, you might want to change that)
  3. I randomized the seed for you so it’s not the same every time.
  4. I changed the member variable to a preload - not an error as such, it’s just a better approach imo.

That’s all I think unless I missed anything.

extends Spatial

onready var main = get_tree().current_scene
var enemy_scene = preload("res://enemy.gd")

func spawn():
    randomize()
    var enemy = enemy_scene.instance()
    main.add_child(enemy)
    enemy.transform.origin = transform.origin + Vector3(rand_range(-15, 15), rand_range(-10, 10), 0)

func _on_Timer_timeout():
    spawn()