How to change something about an instantiated sprite

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

Essentially, I do ‘instantiate(a scene’) and then after ‘add_child(s)’, I can’t seem to change anything about s. I wanted to change its colors and position but it doesn’t do anything. I am relatively new to scripting so I would appreciate any help.

As to why I’m using an instantiated sprite, I’m trying to duplicate my sprite because I want to place it down, so if anyone has any easier solutions to this, I would greatly appreciate it.

:bust_in_silhouette: Reply From: r00sty

When you instantiate the sprite’s scene, assign that to a variable like this:

var Sprite = [Sprite Scene Name].instantiate()

and then using that, you can edits its properties directly like:

Sprite.position.x = 50

thanks for the response but the problem is that I’m trying to make the sprite move in a certain direction after I add the instantiated sprite scene.
I’m using physicsprocess and Sprite.position += vector2(-1, 0) * delta * 14
However it doesn’t do anything?

ShinRamen | 2023-06-01 10:26

Without seeing everything, it would be hard to say exactly why this is not working, but one possibility is that for anything to show up in Godot, you have to add it as a child to an existing node. Here is some basic working code that will get the result that you desire, it isn’t exactly elegant or robust, but perhaps this will get you started:

 extends Node2D

 @onready var SpriteScene = preload("res://Scenes/sprite.tscn")
 @onready var SpriteSpawned : bool = false
 @onready var Sprite

 func _physics_process(delta):

  if Input.is_action_just_pressed("spawn"):
	Sprite = SpriteScene.instantiate()
	add_child(Sprite)
	SpriteSpawned = true
	
  if SpriteSpawned ==  true:	
	Sprite.global_position += Vector2(-1,0) * delta * 14

I made two scenes, the main scene and the sprite scene with just the Godot Icon in it. I attached the script to the root node in the main scene. When I instantiate the sprite, I add it as a child of the main node2D.

r00sty | 2023-06-01 12:51

Alright thanks! What you said made something click in my mind and now everything basically works. I appreciate it

ShinRamen | 2023-06-01 16:54