Game not working when I save, exit out of unity, then come back in.

So I’ve attached my code in screenshots.

Pretty simple code. The obtaining of GameObjects through “FindGameObjectWithTag” in the start method is done just to get some empty game objects that are located in the location that I want certain projectiles to spawn at.

The booleans are true when I want them to be. I ran a debug.log of them and they check out.

The problem is that when I initially open up unity, this script won’t be working. So I remove the script from the game object it’s assigned to in the hierarchy, attach the script again, reassign all the public game objects with some prefabs, and voila, the script works how I want it to. I’ll then save the scene and all the scripts, exit out of unity, open it back up, and it stops working.

For the life of me I can’t figure out how to fix this, and it’s quite annoying to go through the process of fixing it in the way I described above every time I want to work on the game.

Any help will be appreciated.

“this script won’t be working” is’t a helpful description of the problem. You said you checked with Debug.Logs. What have you actually checked? If Unity enters one of the if statements and calls the instantiate method it should create that instance.

Removing and readding the script might indicate a race condition between two of your scripts. So the order in which they are executed might be relevant. This script alone certainly isn’t the problem. This script relies on Player.alive and shootDelay.canShoot. Those are most likely controlled by other scripts. If those scripts also react to Key down / up events of the same keys it depends on which script actually runs first.

We don’t know how your other scripts look like. You have to debug them since you should know how they work. You might want to check your logic and maybe manually set the execution order of your scripts to ensure a certain order.