Objects are not spawning, also, how do I move them?

Godot 4

hi! I try to make a 2D shooter game. Objects are not spawning, and I am not shure what I did wrong.

extends Timer

var cow_01 = preload("res://prefabs/Shooter/shooter_cow_01.tscn")
var coin_01 = preload("res://prefabs/Shooter/shooter_coin_01.tscn")
var coin_05 = preload("res://prefabs/Shooter/shooter_coin_05.tscn")

func _on_timeout():
	randomize()
	var colectables = [cow_01,cow_01,cow_01,cow_01,cow_01,coin_01,coin_01,coin_05]
	var kinds = colectables[randi()% colectables.size()]
	var spawner = kinds.instantiate()
	#spawner.position = Vector2(randi_range(10,990), randi_range(10,590))
	spawner.position = Vector2(randi_range(180,1200), randi_range(1750,1200))
	add_child(spawner)
	_move(spawner)
	wait_time = randi_range(0,1)

func _move(spawner):
	if spawner.position.y >= 0:
		spawner.position.y -= 5

On the other note, how can I move them? I mean, I know how to move things around, I just don’t know how to give every child a direction they would follow till they end thier life cycle. My way makes them wiggle around x axis like they try to cause a triffic accident!

extends Sprite2D

var rnd = RandomNumberGenerator.new()
var horizontal_swift = rnd.randf_range(-100,2000) 

func _process(delta):
	
	_horizontal_swift()
	if $".".position.y >= 0 && $".".position.y <= -100:
		$".".position.y -= 10

func _horizontal_swift():
	horizontal_swift = rnd.randf_range(-100,2000) 
	$".".position.x = horizontal_swift

Processing: Screenshot_۲۰۲۴۰۸۲۹_۱۶۱۰۲۹.jpg…

  1. Does your _on_timeout() method run in your timer?
  2. Open up your Remote tab while the game is running: Do you see any spawned object inside your SceneTree? (maybe their position is somehow Vector2(0,0)?
  3. Can you position the statement add_child(spawner) to be right after you instantiate your spawner and before you set its position?
  4. Is there any red error logs inside your debugger? For example something like cannot instantiate a null value, as spawner is null

I did all the things you sad, unfortunetly it didn’t work:

  1. I think so
  2. There is no object spawning in the tree
  3. Yes, It didn’t help
  4. No errors
    :frowning:

I figured it out! The position coordinates were wrong! Objects were spawning outside the screen

nice, this means that their position was wrong after all.
Thank you for confirming it.

1 Like

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