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.
- Either comment out line 11 using 2 forward slashes (
//
), or remove it. - On line 6, add the access modifier
public
like this:public GameObject capsuleGO;
- Save the file.
- In Unity, select the Main Camera GameObject.
- Drag the Capsule to the Capsule GO field in the Inspector. The following screenshot shows how this is done.
- Click on Play.
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.