- Home /
Creating a looping spawn with an Array
Hi Guys,
I mention this on a previous thread briefly, but thought it would better to make a new thread as it is a different question. I am currently creating a game that has cubes moving in a sequential order. When the first cube gets to a certain location (e.g. x = 40 or fourth cube on the floor) then it should be moved off screen into a "pool" and added to an array called pool. Each time a cube is added to the array, a random one (already in the array) should be removed and moved to the starting position in the "queue" of cubes on scene. A screenshot shown below should help explain this a little clearer. I have chosen to move the cube to pools and use arrays, as opposed to instantiating a cube each time as it performance of the game would decrease substantially.
I currently have two scripts I am using: BuildingController (attached to each building to move them along) and Controller (handle the arrays).
BuildingController:
void Start ()
{
startingPosition = gameObject.transform.position;
}
void Update ()
{
if ((gameObject.transform.position.x >= 25)){
gameObject.transform.position = pool1;
//gameObject.transform.position = begin;
} else {
Move();
}
}
void Move (){
float translation = Time.deltaTime * 2;
transform.Translate (0, 0, translation);
transform.Translate (Vector3.forward * translation);
}
and the Controller script which is meant to supply the arrays and possible queue.
I cannot figure out the way to get this spawn loop working and how the two scripts can link into one another.
If somebody could help me, it would be very much appreciated!
As a side note, I cannot use pool managers or the like downloaded from the asset store.
Many thanks
If you want the cubes to be in an array on one script, you need to make an array of game objects on that script called pool. You can give values to the array by using a for loop and iterating through each object in the array in the start function.
To access that array from the other script, the other script will need a declaration of the class and you can use GetComponent to store it:
private buildingController = BuildingController;
void Start ()
{
//set buildingController to the proper instance
buildingController = find.GameObject("BuildingControllerObjectName").GetComponent<BuildingController>();
}
void Update()
{
//access your array like so
buildingController.pool(1).transform.position = new Vector3( 1,5,6 );
}
@highpockets I don't think you can call an array (or list/`ArrayList`) ;)