Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | hammeron | |
Old Version | Published before Godot 3 was released. |
I’m trying to create instances of Sprite3D and randomize their texture.
I randomize what texture will be set however all instances end up with the same texture. As if all instances share the same configuration, the final texture for every Sprite3D will be the one from the last set_texture() call.
extends Spatial
var textures = [load("res://images/image1.png"), load("images/image2.png")]
func _ready():
var objects = get_node("Objects")
var w = 10;
var h = 10;
for i in range(10):
var newSprite = Sprite3D.new()
newSprite.translate( Vector3( w*rand_range(-1, 1), 0, h*rand_range(-1, 1)) )
var r = randi() % 2
newSprite.set_texture(textures[r]);
objects.add_child(newSprite)
pass
How do I assign different texture by code?
I copy and pasted your code, used images of my own, and didn’t experience the problem you’re describing.
You may be experiencing a problem with your random number choice being the same. Try putting randomize()
into the loop.
avencherus | 2017-04-14 03:06
it’s enough to call randomize()
only once to get different random number.
it’s better to put outside of loop.
volzhs | 2017-04-14 07:30
With print() I could see that the result was random.
Not sure what happened, but I started using material and now it just works…
hammeron | 2017-04-14 14:44