How to change sprite color using Javascript?

I’m trying to apply random color to an object but unfortunately the Random() and Variable() expressions can’t be used
in the ‘Change color of object’ action like so:
Change color of object to “Random(255); Random(255);Random(255)”. or using Variable().

So I’m trying to use Javascript to change the color.
I have no experience with Javascript but I was able to use runtimeScene.setBackgroundColor() to apply a random background color and so I’m looking for a similar (easy) way to do just the same with an object.
I was searching all the classes and methods in the GDevelop documentation but I can’t find anything similar to setBackgroundColor() that can be used on sprites or objects maybe it there but it is not as obvious as ‘setBackgroundColor()’.

So, anybody could help me out with this?
Thanks.

EDIT:// Just solved the problem using ‘ToString()’ expression but I would like to still know how to do it in Javascript, if anybody could help would be great :slight_smile:
Thanks.

https://github.com/4ian/GD/blob/master/GDJS/Runtime/spriteruntimeobject.js#L663
The function is spriteObject.setColor(rgbColor) , rgbColor is a string with format “r; g; b” as in the action :slight_smile:

1 Like

Thanks Lizard it works :slight_smile:

Just to mention in case somebody else also trying to change object color using Javascript, to make it actually work, need to pick the actual object name in to the ‘objects’ variable using the option on the bottom of the code editor and then need to specify the actual instance ID (I guess) between [] after the variable and the actually code is looks like this:

objects[0].setColor("r;g;b");

Maybe it obvious but it was take me almost 2 hours to figure this out :laughing: