>
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!
That's unreadable; please format your code properly.
@Eric5h5 Sorry, first time posting on here! :)
2D arrays could do it.
Answer by sparkzbarca
·
Oct 29, 2012 at 06:49 PM
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!
Hmmm, but what about making simple like:
int array[,] = new int[2,2];
array[0,0] = 1;
array[0,1] = 2;
array[1,0] = 3;
array[1,1] = 4;
and nothing else.
you certainly could. You'll notice if you want to change anything you'll have to go and manually change values perhaps many values over and over.
I used my array so you could change stuff. If your dead on certain your not going to ever need to change the array during runtime or the size or anything later go ahead and use literal values.
I went into detail explaining how to do stuff because clearly the man has never used multidimensional arrays before and seeing the many ways they can be created and modified is appropriate. If you understand what they are already you'll simply create and use them as you did there. :P
Ok now I understand your point. But I'm going to sound a little like a nagger but I would say then, if you do not know what the size going to be at run-time, use a list instead. Well, he got what he needs now. Since you tried and did explain, you get a +1 :)
yea whoops i didnt mean runtime change (impossible of course with static arrays) but simply deciding later on you want the array larger even if its size is still static.
@sparkzbarca thanks for your help! I've been trying for a while now to integrate your answer into my original code and I'm not sure exactly where to put it, as wherever I try to put the code, the editor doesn't seem to like it?
Answer by fafase
·
Oct 30, 2012 at 06:52 AM
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];
Answer by Wasan_Sari
·
Feb 22, 2016 at 03:01 PM
Hello I tried to to initialize my array of arrays using ( myarrays[0] = {0,1,1};) 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.
Crear un arreglo de una clase en otra clase
0
Answers
Keep track of last audio clip played from multiple arrays
1
Answer
How to place a property underneath some other properties (property drawer of serialized object)?
1
Answer
Copying a list of lists of int's in c#
1
Answer
Giving Instantiated Clones their own properties and variables
1
Answer