Create a 2D array (3 columns, 1000 rows)?
I would like to create an empty array (with zeros) in which I would later (in a loop) copy the position of my game object. I know Matlab, so is this possible also in Unity/C#? If yes, how?
Something like that:
for (int i = 0; i < numberOfPoints; i++)
{
float functionXvalue = i * scaleInputRange / numberOfPoints; // scale number from [0 to 99] to [0 to 2Pi]
if (animate)
{
functionXvalue += Time.time * animSpeed;
}
newpos2=new Vector3(initialposition+(i*distanceFromEachOther2),ComputeFunction(functionXvalue) * scaleResult, 0);
plotPoints [i].transform.position = newpos2;
arraySpheresPos [i] = newpos2;
n = n + 1;
The idea woud be that I have 3 columns for (x,y,z) and 1000 rows for each of the positions of my game objects.
Thank you for your help.
Answer by Zeoli · Mar 12, 2016 at 02:08 PM
Why not create an array of Vector3? Or better yet create a list of GameObject? List myGameObjects = new List() ;
Then just store each game object on start in this array. Remove it from the object when the gameobject gets destroyed. You then have a list of all the gameobjects along with their position and rotation. Just use a foreach loop to go through all of them. It then doesnt matter how many gameobjects you have, List<> and the foreach are not fixed in size.
To answer your question you would need to first initialise an array of 1000. Then store the xyz in threes. Then to get the xyz of an object you would need to store the index for that object.
Your answer
Follow this Question
Related Questions
Vector3 is not receiving enough arguments from a another script that gives out transform.position. 1 Answer
Falling object respawner 1 Answer
How to teleport an object to 1 of 8 possible locations on collision? 1 Answer
How i can make that Unity don't round me the numbers of the Vector 3 Array? 1 Answer