Ok, to start off with, im not a programmer, im a designer who is programming, so you’ll have to excuse the possibility that this question may seem trivial or may have already been answered.
So i was basically running through my code trying to clear out any redundancies and change variable types (eg. int to byte) to reduce as much memory or cpu usage as possible.
For most things I have this (for example):
//prefab assigned in unity editor
public GameObject letterASymbol;
//no prefab assigned in editor
public GameObject[] alphabetSymbols = new GameObject [27];
void update ()
{
alphabetSymbols [0] = (GameObject) Instantiate(letterASymbol, Vector3.zero, Quaternion.identy);
}
but then i tried this (attempting to reduce number of GameObject variables)
//All array elements assigned with prefabs in unity editor
public GameObject[] alphabetSymbols = new GameObject [27];
void update ()
{
alphabetSymbols [0] = (GameObject) Instantiate(alphabetSymbols [0], Vector3.zero, Quaternion.identy);
}
It seems to work fine and even did a quick test to see what happens:
//All array elements assigned with prefabs in unity editor
public GameObject[] alphabetSymbols = new GameObject [27];
bool onceTest = false;
void Update ()
{
if (!onceTest)
{
alphabetSymbols [0] = (GameObject) Instantiate(alphabetSymbols [0], new Vector3.zero, Quaternion.identity);
alphabetSymbols [0].GetComponent<SpriteRenderer> ().enabled = true; //as it was default set to false in editor by myself
alphabetSymbols [0].GetComponent<SpriteRenderer> ().color = new Color (1f, 1f, 1f, 0.5f);
Destroy (alphabetSymbols [0]);
alphabetSymbols [0] = (GameObject) Instantiate(alphabetSymbols [0], new Vector3.zero, Quaternion.identity);
onceTest = true;
}
}
When its instantiated the second time, the alpha is still at 0.5f and stuff.
I guess my question is, if you’re careful about making sure all the parameters are reset (colour alpha, position, scale, etc) before you destroy it (if thats what you want of course), is this better memory/cpu wise? Or am I quibbling about negligible differences? Is this a better way in terms of keeping code less cluttered?