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 because Unity has done the assignment internally for us. Also, this doesn't change LearningScript in any way.

Pop quiz – understanding communication between objects

Q1. What is Dot Syntax, and what does it allow you to do?

Q2. When an object is assigned to a variable, what is actually stored in the variable?

Q3. Are there any limits to using Dot Syntax when trying to access variables and methods?

Q4. What is another way to assign GameObjects to variables besides writing code?

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

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