Chapter 15, example code crashes

I have traced the crash due to an optional being unwrapped, and finding a nil. What I can’t figure out is why it is crashing. In Chapter 15, in the gameReset function, it crashes as soon as you set the velocity to 0 using the SCNVector3Zero. If you comment out that line and the next line which resets the balls position, it no longer crashes the game until you tap on the display.

function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> 
of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, 
Swift.StaticString, Swift.UInt) -> ()).(closure #2)
->  0x10053a5dc <+44>:  brk    #0x1

I tried completely pasting in the solution files GameViewController.swift file, and same thing. So the problem is in the game scene editor?

fatal error: unexpectedly found nil while unwrapping an Optional value

The solution project itself compiles and plays fine…
Ran it in the sim this time, the error message is a little more detailed,

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Oh Geez! I found the problem, the error is due to editing in the book. In an earlier chapter, in the scene editor, you had us make a folder called ball, and place the ball reference node in that folder. If you remove that ball folder, and just place the ball reference node in the scene editor, just like how the solution project has it, it does not crash. The nil was Xcode not being able to find the ball!

I seem to have the same problem here (or at least a very similar one), but without a solution, unfortunately.

I’m on page 260. When I run the project, I get a crash at the point where resetGame() is called.
It turns out that the crash is caused by a forced unwrap of an optional which is actually nil; this happens when resetGame() tries to access the ballNode’s physicsBody property in order to set its velocity to zero. So, at this stage the ballNode has no physicsBody, which seems strange.

I then put a breakpoint in setUpNodes to see what was going on there. If I po ballNode.physicsBody after the first line, where ballNode is set, I can confirm that physicsBody is nil (although ballNode itself actually exists). It seems that the Scene editor can find the ball object and create a reference node from it, but somehow the physicsBody isn’t getting set.

In my case, I don’t think this is related to the folder structure; I’ve changed mine so that it is exactly the same as the example code, and the problem persists.

The supplied example code runs just fine. I’ll keep looking at this, but it seems very odd.

Perhaps a little perversely, I actually like it when things go wrong - the digging around you do in fixing things is often a lot more instructive than the process of just typing in code that works first time!

Anyway, a lttle hint for anyone who runs into a similar problem at this point: the error in my case was that the ‘ball’ reference node in the main game scene had been renamed ‘ball’. Keep it as the default name, and it all works properly.

This is actually already covered in some posts in the ‘Errata’ section of the forum, but it’s probably worth repeating it here.

1 Like