0 votes

so i have a series of boxes inside a box container
i want to give each of them one of six colors when scene loads up
here is my code

extends Node2D

var color_one = Color(255, 0, 0, 1) 
var color_two = Color(0, 255, 0, 1) 
var color_three = Color(0, 0, 255, 1) 
var color_four = Color(0, 0, 0, 1) 
var color_five = Color(255, 0, 255, 1)
var color_six = Color(255, 255, 0, 1) 

var colors_array = [color_one, color_two, color_three, color_four, color_five, color_six]

func update_colors():
    var nodes = get_node("box").get_children()
    for box in nodes:
        var current_color = colors_array[randi() % colors_array.size()]
        box.modulate = Color(current_color)
func _ready():

but strangely its generating the same colors every time!!!

Godot version 3.3.stable.official
in Engine by (22 points)

1 Answer

+1 vote
Best answer

You need to call randomize() to seed the rng, or you will get the same "random" sequence every time you run.


You can add it to _ready().

by (21,716 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.