Division By Zero in operator '%' Error

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

So I having this error “Division By Zero in operator ‘%’”, my intention is to get the quantity of children nodes and using them as a parameter to randi

extends Node
func create_meteor(value):
	return get_children()[value].duplicate()
func random_meteor():
	return get_children()[randi()%get_children().size()].duplicate()

It’s implying that get_children().size() is equal to 0. Does this node have children attached?

literalcitrus | 2018-02-14 03:21

thank you for, your reply


cyromago2 | 2018-02-14 03:33

:bust_in_silhouette: Reply From: literalcitrus

Try checking the stack trace when you receive the error, it might point you to where the random_meteor() function is being called where get_children().size() is returning 0.
Are you instancing this scene via code in another scene at all? If you are, you should make sure that random_meteor() is called after you add the scene to the tree (via add_child() in the other script). I’m not 100% sure but that’s where I would look next.