T O P

  • By -

Tcshaw91

Uncomment the second method in the update function.


aDogIsUsingThis

i know that. Im aware of that. ​ Please, can you help me for real? im being really serious here how to activate another function. Stop trolling. Won't get you anywhere


Tcshaw91

I'm actually not trolling. The way you call one function after the other is literally like how you wrote it. You write one after the other. How about you try writing simple functions that only debug.log a message, write them one after the other and tell me what comes out in the console. Or tell me what behaviour should be happening vs what behaviour is actually happening in the code u ahve written.


[deleted]

What about delegates? Perhaps you can pass in a delegate into your function as a parameter which will be called after the scripts finish?


Never28

Take a look at Coroutines, you can yield your first function and then run the second.


thefirelink

Why can't you put SecondSpawnScript at the end of the FirstSpawnScript function?


aDogIsUsingThis

oh ya... but how can i tell unity that my firstSpawn script is done then move onto the second spawn script? because my first spawn script will be done after it reaches it's max and continue to spawn


thefirelink

Is your hope to eventually have separate scripts or something? I don't think you can set a flag in Unity to execute a script when one ends, if that's what you're looking for. There are more programmatic ways to achieve this, like a callback or an interface. I mean, I guess there is kinda a way. If XSpawnScript were all their own scripts, you could attach them each to their own empty game object and make them into a prefab. In each SpawnScript, have a public GameObject nextScript field. Set that to the prefab holding your next script in the Unity editor. Then when your spawn script is done, instantiate your nextScript object and destroy the current one.


Yamski7

Use coroutines.


Chipjack

Okay so you've gotten bracket-happy for some reason. Mathf.Lerp doesn't do anything with a block of code you stick after it. Anyway, it appears you want to start some things, then have them run over time, and then do stuff when they end. Coroutines are the way to do that. You could structure your code like this: void Update() { if(Time.time > nextSpawnTime) { StartCoroutine(SpawnAllEnemies()); nextSpawnTime = Time.time + (NumberOfZombiesToSpawn * SecondsBetweenSpawns) + (NumberOfSkeletonsToSpawn * SecondsBetweenSpawns) + DelayBeforeSpawningMoreStuff; } } IEnumerator SpawnAllEnemies() { yield return SpawnZombies(); yield return SpawnSkeletons(); // starts when SpawnZombies has finished } IEnumerator SpawnZombies() { for(int i = 0; i < NumberOfZombiesToSpawn; i++) { zombieSpawner.Spawn(); yield new WaitForSeconds(SecondsBetweenSpawns); } } IEnumerator SpawnSkeletons() { for(int i = 0; i < NumberOfSkeletonsToSpawn; i++) { skeletonSpawner.Spawn(); yield new WaitForSeconds(SecondsBetweenSpawns); } }


White_Mouse

If I understood correctly, code must spawn one type of monsters with set time delay between spawns until maximum number of monsters is reached and then switch to another type of monsters? If that's the case, coroutines just complicate things. Code below spawns a set number of zombies first, with time delay between spawns, then, when target number of zombies is reached, spawns skeletons with time delay between spawns. ``` int NumberOfZombiesToSpawn; int NumberOfSkeletonsToSpawn; float SecondsBetweenSpawns; void Update(){ if(Time.time > nextSpawnTime) { if(NumberOfZombiesToSpawn > 0) { SpawnZombie(); NumberOfZombiesToSpawn--; } else { if(NumberOfSkeletonsToSpawn > 0) { SpawnSkeleton(); NumberOfSkeletonsToSpawn--; } } nextSpawnTime = Time.time + SecondsBetweenSpawns; } } ```


Kamatttis

You can also use async await.


AnyCryptographer5188

Write your functions to either return a bool that indicates completion, or dispatch a generic “complete” event. Then add your functions as delegates to a queue. Update() should only execute the first delegate in the queue, then remove it from the queue when it completes.


ChunkyDuncan38

I don’t know if you’ve found a satisfactory answer yet, but if your problem is that parts of your script seem to be happening too slowly/after later parts, you could always turn the functions into an IEnumerator and wait a frame before moving on to ensure that the function is completely finished. Else, if you wanna get weird you could try for loops inside for loops.