Make an array of game objects that I can loop through

I’m trying to find a way to make an array in my script that goes through each of the game objects in my scene and randomly sets their localEulerAngles so that each one is positioned in a noticeably different manner. As it stands, I have the objects taking on a different rotation every time I launch the test but differences in their rotations is only slightly different between the objects and I’m looking for a way to make it so that the difference in rotation is obvious.
This is the current script I’m using to control the objects.
https://playcanvas.com/editor/code/529780?tabs=10799160,10786903

Is the problem creating the array? Or making the rotations noticeably different or both?

In a way both. If I can get the array working, then I can make it loop through each object and change the initial rotation in a way that is unique to each entity.
I know roughly it would be something like:
var numDice = 5;
for(i =0; i < numDice; i++)
{
set values here
}

I’m just not sure how to get the numDice to count the dice objects in the scene.

There are a couple of ways to do this, the easiest here would probably be giving each die the same tag name (eg ‘dice’) and using this.app.root.findByTag('dice') will return the array of entities that have the tag ‘dice’.

As your dice don’t get destroyed, created etc, you can call this once in the initialise function and cache the array to be used multiple times later.

2 Likes

That seems to have done it. If any problems come up, I’ll post them here.