Hello!
So I have made a starfield, loaded procedurally by simply instantiating a star in a random position when the scene loads.
To make the starfield infinite, I added a script to each individual star prefab. The code says if it gets too far away from the camera on either the X or Y axis, move it to the other side of the camera. This works pretty well to create an infinite starfield.
if (transform.position.y < cam.transform.position.y-7)
{
transform.position.y = transform.position.y+14;
}
if (transform.position.y > cam.transform.position.y+7)
{
transform.position.y = transform.position.y-14;
}
if (transform.position.x < cam.transform.position.x-12)
{
transform.position.x = transform.position.x+24;
}
if (transform.position.x > cam.transform.position.x+12)
{
transform.position.x = transform.position.x-24;
}
The problem is that it clearly isn’t very efficient having this code running on 700 seperate objects. I have been looking into controlling all the objects from one script, probably using arrays, but I can’t find a way to apply logic to all GameObjects inside an array. How would I do this? I can apply it to a single object in the array by using something along the lines of “myArray [1].transform.position”, but not how to do it for every object in the array. Thank you!