It continuously calls the function once my character hits level 3. This obviously makes sense, but I am looking for the easiest way to only call it once when my character hits level 3. I am sure I could think up some complicated and messy code to accomplish it, but I am also sure there is a much better way to do it, and I am looking for that because I want my game to be scalable. I am also looking to learn anything I can.
I would say, move it out of Update. Whatever case triggers where your level changes you should have a call right after to trigger your EnemySpawner code. There is no reason to have it in Update since you already have a condition that you should be tracking.
Some point all enemies are dead.
Level changes
Call spawn
For anyone who reads this, this is the “solution” I came up with. Each time you level the counter variable is set to 50. For when the character levels to level 3, the counter starts counting down and triggers my function when it hits 49. This will be scalable, I will just have to make the trigger 48 for the next level I want to call my function, and so on.
To run something once, you just need a bool variable like “hasRun = false;” and you’ll check if that’s false && your other condition is true. Once you run the function, set it to true and it won’t fire again. You can then set it to false as needed, like if another level happens. This is a common pattern.
Your method with the 49, 48, etc. will work, but isn’t necessary as long as the resulting call is the same each time. If it were to be different, that could identify what to do. But the level number would likely do that as well.
Don’t reference GameObject, if EnemySpawner is prefab or GameObject on the scene then you can make field with type EnemySpawner and drag GameObject it in, it will be correctly referenced if GameObject has EnemySpawner component on it. As @Brathnann said you probably don’t need to call the function in Update. Try to make everything event like, something happened → code called one time.
Anyway, the solution: