- Home /
Easiest way to have a large number of variables
I am looking to have a lot of objects in my game, and I do not know how I should have all of those coordinates stored into variables, would the best option be to create and array of Vector3 coordinates or is there a faster, less memory consuming way to do this? thanks!
It depends what you wish to accomplish with the coordinates.
Note that usually faster means more memory and vice versa in data structures.
ahh, well then whatever is faster, I have tried creating an array but that would be a giant array, and I beleive that arrays may be to slow. With each coord I only need to have one piece of info stored, I guess I can just try a 3d array.
But what do you wish to do with the variables?
Saying it needs to be faster is not enough information. One should know what is the purpose of keeping the variables in order to decide what approach would be the fastest one considering one's needs.
A built-in array is probably the fastest way to store lots of data according to this doc - http://unity3d.com/support/documentation/ScriptReference/Array.html
Answer by aldonaletto · Aug 04, 2011 at 03:12 PM
You should use a Vector3 array. It's a piece of cake for any game engine - every game object created occupies hundreds of bytes, while each Vector3 element occupies only 12 bytes. It won't even scratch the available memory.
You can use built in arrays, but you must know the array length beforehand. I did it once: I had a main control script, and a few instructions in each trackeable object's script to count itself and fill the array.
This is the control script (Control.js)
static var numObjects = 0; // objects will count themselves here static var positions: Vector3[]; // array is actually allocated in Start
function Start(){ positions = new Vector3[numObjects]; // at Start all objects were counted } In the object script, the object counts itself in Awake, and fills its position in Update only once:
var myNum: int; // object ID number private var setPos = true; // tells the object to save its position in the array
function Awake(){ myNum = Control.numObjects; // object records its ID number Control.numObjects++; // and increments object count }
function Update(){ if (setPos){ // need to set position? Control.positions[myNum] = transform.position; setPos = false; // do it only once in life! } ... }
Your answer
![](https://koobas.hobune.stream/wayback/20220612170253im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reading Other Scripts stats 3 Answers
"Import Assets" Speed, need a better procesoor? 0 Answers
is it better to use MeshFilter[] or three separate MeshFilter var's 1 Answer
One texture loaded, used in multiple terrains, Possible? 0 Answers
texture memory question - colour tinting a single texture multiple times? 1 Answer