-2

I'm coding a space invaders type app right now, and I recently ran into a really annoying fatal error that freezes my game. I'm trying to get a laser to take the enemy off of the screen, except as soon as the player, enemy, or laser come in-tact with each other, the game then freezes. I've tried force unwrapping it and everything, but nothing seems to work. Here is the area the error is at the moment: if body2.node == nil{ if ((body2.node!.position.y)) > self.size.height{ The error is in the line with if((body2.node. My error states: "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value". FYI, I'm using the solo mission tutorial by Matt Heaney apps on YouTube to code.

  • You are force unwrapping `body2.node` inside a condition where you already know that body2.node is nil : `if body2.node == nil`. Logic error. This crash is expected. Maybe you wanted to do the opposite? `if body2.node != nil` – Eric Aya Mar 25 '21 at 12:09

1 Answers1

0

That error means, that one ore more objects of you're code wasn't initialized and may be sometimes tricky to fix. For your code please check if you initialized the object body2 or try to provide a default value using ??. Best regards