Time for action – trying drag-and-drop to assign a GameObject

Let's change a few lines of code in LearningScript to show how to assign the Capsule GameObject to the variable capsuleGO using drag-and-drop.

  1. Either comment out line 11 using 2 forward slashes (//), or remove it.
  2. On line 6, add the access modifier public like this: public GameObject capsuleGO;
  3. Save the file.
  4. In Unity, select the Main Camera GameObject.
  5. Drag the Capsule to the Capsule GO field in the Inspector. The following screenshot shows how this is done.
  6. Click on Play.
Time for action – trying drag-and-drop to assign a GameObject

What just happened?

The Capsule GameObject is now assigned to the capsuleGO variable. We didn't have to write the code ...

Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.