Hello Peeps, I would like to bring up a new topic as a new C# and unity3d user i found some fundamentals not very clear ( probably i misunderstand sth ).
This is about code execution behavior , I made a small project and I am having a bit execution order ( or better say disorder ) issues that causes many bugs.
After the first glance of the Unity Manual things came pretty simple about code execution order but lets make things clearly.
CASE A. Given this Start Function the function
void Start () {
posicionpieza (origin);
creatotemprimero ();
target.piezastarget = cuantaspiezas;
makestart ();
StartCoroutine (Lfoundry());
}
I need that function makestart its executed just After creatotemprimero its already finished. the thing is Creatotemprimero() has a lot of Resources.load and instantiate prefabs inside a for loop it ( lets say its a heavy function ).
The question is even i write it on code before its executed at the same time or it may happen that the for loop is long and the code continues loading the rest and the function makestart() is essentially needed to manage the resources loaded by the function before … ( is only coroutines the only thing to manage this??? )…
CASE B. More deep core routines.
for (int lvl = 0; lvl < totemlevel ; lvl++){
for (int y = 0; y < lapieza2.Length; y++) {
// las tres piezas
string el_color = color [Random.Range (0, color.Length)];
switch (y) {
case 3:
sumandox = 1.41f;
sumandoz = -1.41f;
break;
case 2:
sumandox = -1.41f;
sumandoz = -1.41f;
break;
case 1:
sumandox = -1.41f;
sumandoz = 1.41f;
break;
case 0:
sumandox = 1.41f;
sumandoz = 1.41f;
break;
default:
sumandox = 0;
sumandoz = 0;
break;
}
lapieza2 [y] = Resources.Load ("Cubo" + el_color + "Model") as GameObject;
Instantiate (lapieza2 [y], new Vector3 (origin.x + sumandox, origin.y + (lvl * 2.8f), origin.z + sumandoz), Quaternion.identity);
GameCube gamecube = lapieza2 [y].GetComponent<GameCube> ();
gamecube.elcolor = el_color;
lapieza2 [y].tag = "litrick";
}
}
foreach (GameObjectpoliceinchekator) {
piezasdestruidas += 1;
elsubida = police.GetComponent<GameCube>().cubelevel;
elpos = police.GetComponent<GameCube>().posde4;
sumapuntos += (police.GetComponent<GameCube> ().CubePoints) * (x+j);
gelocatil(elpos,elsubida);
Destroy (police, 1.7f);
}
I have again one big For lets say totem level its 2000 or 1 million or 1 billion .so the of is very huge … then i call a foreach… Is the foreach always wait the for function ends? or could be executed at same time? this things is killing me
CASE C: Coroutines
Lets say coroutines exist for execution order something
so I am used to concatenate coroutines and yield return 0, at start so its supposed to wait next frame to load.
IEnumerator Lfoundry(){
yield return StartCoroutine(Gamecheck ());
if (Lfound == 0) {
startener = 0;
} else {
startener = 1;
cambiomatches ();
}
}
void cambiomatches(){
GameObject[] chekator;
chekator = GameObject.FindGameObjectsWithTag ("match");
foreach (GameObject police in chekator) {
string el_color1 = color [Random.Range (0, color.Length)];
GameObject sustituto1 = Resources.Load ("Cubo" + el_color1 + "Model") as GameObject;
Instantiate(sustituto1, police.transform.position,police.transform.rotation);
Destroy (police);
}
StartCoroutine (Lfoundry());
}
Is this possible to cambiomathes will be excuted at same time that foundry? I have the feeling that i have to use only Enumerators to get the code well executed but a voice inside is telling me i am doing something wrong