So I have this project running, in which I have an array of objects. Every object has a time variable and a couple more determining the other values used to spawn in a GameObject. now what i need to do is loop over that array and Instantiate all GameObjects at the right time. How I thought of doing it now was just having a loop, that counts the time difference between last time it ran and adds all previous iterations up, then checks if that time is higher than the time value in the array, if so go to the next object and check, if not just do nothing and keep looping...
This method would probably hold down the entire game and be very inefficient. So I need to figure out how to approach this problem logically. I have read a bit about tasks... I would probably want to use a Async Task? and in there use Task.Delay(); to time the method calls to spawn the GameObjects? Would that work? How would I approach that async Task if it is the best solution?
myObject[] myObjectArray; //has to come in from call
float currentTime = 0;
for (int i = 0; currentTime < myObjectArray[myObjectArray.Length-1].time; i++) { // or i < array.Length
Task.Delay(TimeSpan.FromSeconds(myObjectArray[i].time - currentTime)); //wait until next note
currentTime = myObjectArray[i].time; //add
InitializeThisObject(myObjectArray[i]);
}
this makes sense in my head, but I have never worked with tasks like this, so my logic with how to use them would probably flawed if I were to try and put that loop into a task now... So can someone help with how to make this task? I appreciate any help. Even maybe just confirming whether or not the task is the right way to go for this would help.
Oh and... would a coroutine also do the desired job? could I do that "wait X time, then do the next object" in a coroutine better maybe?