Array of Arrays

Was wondering if anyone could help, I’m trying to create a Temple Run style iOS game, so I’m recycling the platforms at the moment rather than using Instantiate and Destroy. At the moment, the platforms are created at Start, and are random, but then are recycled in the same order after the first 20. I want to create an Array of Arrays, so have 4 different groups of say 20 different models of platforms, which a group is chosen using randomNumber (Element 0-3), and then use randomNumber again to chose out of which of those 20 platforms to select randomly from the chosen group. This is my code at the moment:

using UnityEngine;
using System.Collections.Generic;

public class PlatformManager : MonoBehaviour {
	
	public Transform[] prefabs;
	public Transform[] platform1;
	public Transform[] platform2;
	public Transform[] platform3;
	public Transform[] platform4;
	
	
	
	public int numberOfObjects;
	public float recycleOffset;
	public Vector3 minSize, maxSize, minGap, maxGap;
	public float minY, maxY;

	
	
	private GameObject[] TileGameObjects;
	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;

	void Start () {
		
		objectQueue = new Queue<Transform>(numberOfObjects);
		for(int i = 0; i < numberOfObjects; i++){
			int randomNumber = Mathf.Abs (Random.Range(0, prefabs.Length));
			objectQueue.Enqueue((Transform)Instantiate(prefabs[randomNumber]));
		}
		
			
		nextPosition = transform.localPosition;
		for(int i = 0; i < numberOfObjects; i++){
			//int randomNumber = Mathf.Abs (Random.Range(0, prefabs.Length));
			//objectQueue.Enqueue((Transform)Instantiate(prefabs[randomNumber]));
			Recycle();
		}
	}

	void Update () {
		if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
			Recycle();
		}
	}

	private void Recycle () {
		Vector3 scale = new Vector3(
			Random.Range(minSize.x, maxSize.x),
			Random.Range(minSize.y, maxSize.y),
			Random.Range(minSize.z, maxSize.z));
			

		Vector3 position = nextPosition;
		position.x += scale.x * 0.5f;
		position.y += scale.y * 0.5f;
		
		Transform o = objectQueue.Dequeue();
		o.localScale = scale;
		o.localPosition = position;
		
		
		objectQueue.Enqueue(o);
		Debug.Log("alert!");
		
		nextPosition += new Vector3(
			
			
			Random.Range(minGap.x, maxGap.x) + scale.x,
			Random.Range(minGap.y, maxGap.y),
			Random.Range(minGap.z, maxGap.z));
	
		
		if(nextPosition.y < minY){
			nextPosition.y = minY + maxGap.y;
		}
		else if(nextPosition.y > maxY){
			nextPosition.y = maxY - maxGap.y;
		}
	}
}

I have only created the variables for Platform1, 2, 3 and 4, but not sure how to integrate them inside the prefabs Array.

If anyone could help I would really appreciate it!

yea you need an array where each item in the array is an array itself

for example

//creates an array of arrays
int[][] myarray;

//creates an array which contains 20 arrays the arrays are all empty and unitialized, they will need to be initialized, there is currently just 20 starting points reserved in memory. They point to nothing really.
myarrays = new int[20][];

//the first array is size 3 with the element 0 & 1 & 1
myarrays[0] = {0,1,1};

myarrays[0][0] = ? //based on the statement above it equals 0
myarrays[0][1] = ? //based on the statement above it equals 1
myarrays[0][2] = ? //based on the statement above it equals 1 as well


//the following code will take every array in myarrays and make them equal to arrays of size 10, the arrays are empty but now they point to something, its probably zero by default due to internal code.
for (int x = 0; x <= myarrays.size(); x++)
{
    myarrays[x] = new int[10];
}
//technically the code above still hasnt set what the value of each element in the arrays are, it simply now says its size is definitely 10, internally the code has probably set those memory values to zero however to help prevent unexpected errors, that's not guaranteed I don't think however and it would probably be best to set each value to be certain different machines and things dont do things differently and create unexpected and hard to find errors.


//the follow code will create an array that contains 20 arrays, each of those arrays are of size 10, and each of them will have the value set to 0;

int[][] myarrays = new int[20][];

//careful on this for statement, it is less than not less than or equal too, this has to do with size starting to count from 1 and x starting to count from 0. You must start from 0 because the first index in an array is 0.

for(int x = 0; x < myarrays.size(); x++)
{ 
     myarrays[x] = new int[10];
   for (int y = 0; y < myarrays[x].size(); y++)
    {
       myarrays[x][y] = 0;
    }
}

//Do note that i used the size function to get the size, thats because you shouldn't used numbers like 20 or 10 even if thats what you think you want. You should declare a constant called NUMBER_OF_ARRAYS and NUMBER_OF_ELEMENTS_IN_ARRAYS or something like that and substitute those that way you can change the size of the arrays with a few keyboard strokes.

mark as answered and have a nice day!

using UnityEngine;
using System.Collections;
using System;

public class WaitC : MonoBehaviour {
    const int row = 2;// My values are smaller than 4 and 20...
    const int col = 3;
	Transform [,]array = new Transform[row,col];
	void Start(){
		GameObject[] obj=GameObject.FindGameObjectsWithTag("Platform");
		array[0,0]= obj[0].GetComponent<Transform>();
		array[0,1] =obj[1].GetComponent<Transform>();
		array[0,2] =obj[2].GetComponent<Transform>();
		array[1,0] =obj[3].GetComponent<Transform>();
		array[1,1] =obj[4].GetComponent<Transform>();
		array[1,2] =obj[5].GetComponent<Transform>();
		for(int i = 0;i<2;i++){
			for(int j = 0; j<3;j++)print (array[i,j].transform.position);
		}
	}	
}

This is an example with 2Darray of Transform. Now for choosing random parts, if I understood right you want to choose a random index and then a random part.

int a = Random.Range(0,row); int b = Random.Range(0,col);
Transform newPlatform =  array[a,b];

Hello
I tried to to initialize my array of arrays using ( myarrays[0] = {0,1,1}:wink: in c# but it keep giving me errors on the symbol { .

However I manage to do that by using :
jaggedArray[0] = new int { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int { 0, 2, 4, 6 };
jaggedArray[2] = new int { 11, 22 };

see this website: