Godot Version
4.5
Question
So, im trying to give the tree one of three random options for a texture, but the problem is that whenever my code gets to the part where i want to assign the texture to the newly created node it says “Invalid assignment of property or key ‘Texture’ with value of type ‘String’ on a base object of type ‘Sprite2D’.”, which is confusing to me because how else am i supposed to put the texture in the code?clicking and dragging the png into the code is how i got that so i assumed itd work
If you share code, please wrap it inside three backticks or replace the code in the next block:
extends Node2D
var Treever = 0
#components of timer(from tutorial. thanks youtube!)
var Cd = true
var countdown_complete = false
var Timeleft = 0.0
func NewNode(node_2d: Node2D) -> Node2D:
var new_node2d: Node2D = node_2d.duplicate()
#make a copy of the node you give it with random size
new_node2d.scale = Vector2(randf_range(7.0,13.0), randf_range(7.0,13.0))
new_node2d.position = Vector2(randf_range(0,1150), randf_range(0,650))
#Trying to get the Sprite2D of the new copy
var node2child : Sprite2D = node_2d.get_child(0)
#Trying to make sure the value of node2child is the Sprite2D of the new node ( it prints sprite2d for me)
print(node2child)
#random number to choose the version of the tree art to use
Treever = randi_range(1,3)
#i was printing Treever to try and find out where things were going wrong earlier.
print(Treever)
#Things usually break right here, it says "
#Invalid assignment of property or key 'Texture' with value of type
#'String' on a base object of type 'Sprite2D'."
if Treever == 1:
node2child.Texture = "res://Png/TreeVerOne.png"
if Treever == 2:
node2child.Texture = "res://Png/TreeVerTwo.png"
if Treever == 3:
node2child.Texture = "res://Png/TreeVerThree.png"
add_child(new_node2d)
return new_node2d
func _ready() -> void:
pass
func _process(_delta: float) -> void:
if Cd == false :
NewNode($Tree)
Cd = true
elif Cd == true:
if Timeleft <= 0.0 :
Cd = false
Timeleft = 3.0
print("Ding!")
else:
Timeleft -= _delta