Debugging

Unity provides a great interface in the editor for watching and editing the state of game objects and components while your game is running. While this will likely be the way you can debug most of your game, you may still encounter times where logic in a script needs a finer look. Fortunately, Unity also provides an excellent set of tools to allow you to debug your scripts in the editor while your game is running. Let's look at how you might start a debugging session:

  1. Open Unity with an empty project.

    Note

    If you are using Visual Studio, this exercise assumes that you have already installed the tools extension and configured the editor preferences.

  2. From the menu, select Assets | Import Package | Custom Package... and navigate to the Chapter_10_Assets folder in the book's downloaded source code. Select Chapter10.unitypackage and click on Open.
  3. The package is small, so it should import quickly. Click on the Import button on the Import Unity Package dialog to continue.
  4. Locate the Main scene in the Assets/Chapter 10/Scenes folder in the Project window and double-click on the scene to open it.
  5. Locate the RotateObject script in the Assets/Chapter 10/Scripts folder in the Project window and double-click on the script to open it in your editor of choice.

    Note

    We will demonstrate the use of the debugger for MonoDevelop and Visual Studio. If you are using another code editor, the process may be different.

  6. Set a breakpoint on a single line of code inside the Update method, as shown in the following screenshot:

    Debugging

  7. The next step will depend on the editor you are using. Follow the directions for your selected editor to start debugging:
    • MonoDevelop: In the toolbar, press the Play button to start debugging. The Attach to Process dialog will show; now select the Unity process your project is loaded in and click on Attach.

      Tip

      If you have multiple Unity editor instances running, it may be a bit of trial and error to find the right process to attach to. Pay special attention to the process ID after you attach to the correct process. Of course, the alternative is to only run a single instance of Unity.

    • Visual Studio: In the toolbar, press the Play (Attach to Unity) button to start debugging. Visual Studio is smart enough to attach itself to the Unity editor on its own.
  8. Return to Unity and run the project by pressing the Play button.
  9. In a very short time, you will be taken to your script editor and the breakpoint you set earlier will be highlighted. At this point, use your mouse to hover over the text and inspect any variable properties, as shown in the following screenshot:

    Debugging

    An example of breakpoint and type inspection in MonoDevelop and Visual Studio

There are a number of other debugging options you can employ, such as watches, but the preceding exercise will get you started. Of course, there are times when you are deploying to a mobile platform where you want to debug directly on the device; we will cover that in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.13.255