Prospector Solitaire

So, if you know Jeremy Gibson, he has 2 books about entering to Level Game Design in Unity. In the book, there is a prototype tutorial for a solitaire game.

well, I have an error on the script Prospector.cs regarding a variable that derives from the script Deck.
The error is a NullReference, here is a snip of the code.

The error is in the line of cp.faceUp = tSD.faceUp;

        foreach(SlotDef tSD in layout.slotDefs)
        {
            // ^ iterate through all the SlotDefs in the Layout.slotDefs as tSD
            cp = Draw(); // Pull a card from the top (beginning) of the draw Pile
            cp.faceUp = tSD.faceUp; // Set its faceUp to the value in SlotDef
            cp.transform.parent = layoutAnchor; // Make its parent LayoutAnchor
            // This replaces the previous parent: deck.deckAnchor, which
            // appears as _Deck in the Hierarchy when the scene is playing.
            cp.transform.localPosition = new Vector3(
                layout.multiplier.x * tSD.x,
                layout.multiplier.y * tSD.y,
                -tSD.layerID);
            // ^ Set the LocalPosition of the card based on slotDef
            cp.layoutID = tSD.id;
            cp.slotDef = tSD;
            // CardProspectors in the tableau have the state CardState. tableau
            cp.state = eCardState.tableau;

            tableau.Add(cp); // Add this CardProspector to the List<> tableau
        }

I don’t know what did I do wrong. But if someone is so gentle to help me I will link my GitHub repo here.:
https://github.com/Dreaddisk/Prospector-Solitaire

Well, if the error is on that line, then either cp is null, or tSD is null. So your next step is to find out which. Add these lines right before that one:

if (cp == null) Debug.Log("cp is null");
if (tSD == null) Debug.Log("tSD is null");

Then run again, and look in the log. Which one is it? Then ask yourself: how could that thing be null in this situation?

Keep working backwards, collecting one clue at a time, until you’ve traced it back to the root cause. This process is the core of programming.

Yeah, I would have like to debug it on my own, but like the project is entitled to a book I didn’t want to alter it that much. I will try it, though.

It’s impossible to program without occasionally hacking the code to figure out what’s going on. This experience is far more valuable than anything you’d get out of following the directions in a book correctly. :slight_smile: Don’t worry, once you figure it out, you’ll be able to put everything back and continue on with the book.

From what you posted it looks to me the the problem is Draw() is not returning a valid class to assign to cp. You might want to make sure all fields are filled in the inspector on the scripts. Your screenshot shows the error on the Prospector script at line 92, so I’m a bit confused…