6. Accessing Components

uke 的头像

As a game object can have multiple scripts (or other components) attached, it is often
necessary to access other component’s functions or variables. Unity allows this via the
GetComponent() function.
We’re now going to add another script to our spotlight which will make it look at Cube1
whenever the jump button (spacebar by default) is pressed.
Let’s think about this first, what do we want to do:
1. Detect when the jump button has been pressed.
2. When jump has been pressed make the spotlight look at Cube1. How do we do this?
Well, the Follow script contains a variable “target” whose value determines which game
object the spotlight should look at. We need to set a new value for this parameter. We
could hardcode the value for the cube (see the section ‘Doing it with code’ later), however
we know that exposing the variable and assigning this via the GUI is a better way of doing
this.
- Create a new Javascript and name it Switch. Add the following code to Switch.js:
var switchToTarget : Transform;
function Update () {
if (Input.GetButtonDown("Jump"))
GetComponent(Follow).target = switchToTarget;
}
Notice in particular how Follow is the parameter to GetComponent(), this returns a
reference to the Follow script which we can then use to access its “target” variable.
- Add the Switch script to the spotlight and assign Cube1 to the switchToTarget
parameter in the Inspector View.
- Run the game. Move around and verify that the spotlight follows you as usual, then hit
the spacebar and the spotlight should focus on the Cube1.
Doing it with code
Earlier in the tutorial we mentioned that it would be possible to assign the variables via
code (as opposed to the Unity GUI), let’s take a look at how you would do that.
5
Remember this is only for comparison, assigning
variables via the GUI is the recommended approach.
The problem we were interested in earlier was how
do we tell the spotlight to look at Cube1 when the
jump button was pressed. Our solution was to
expose a variable in the Switch script which we could
then assign by dropping Cube1 onto it from the Unity
GUI. There are two main ways to do this in code:
1. Use the name of the game object.
2. Use the tag of the game object.
1. Game object name
A game object’s name can be seen in the Hierarchy View. To use this name with code we
use it as a parameter in the GameObject.Find() function. So if we want the jump button to
switch the spotlight from Main Camera to Cube1, the code is as follows:
function Update () {
if (Input.GetButtonDown("Jump"))
{
var newTarget = GameObject.Find("Cube").transform;
GetComponent(Follow).target = newTarget;
}
}
Notice how no variable is exposed as we name it directly in code. Check the API for more
options using Find().
2. Game object tag
A game object’s tag is a string which can be used to identify a component. To see the
built-in tags click on the Tag button in the Inspector View, notice you can also create your
own. The function for finding a component with a specific tag is GameObject.FindWithTag
() and takes a string as a parameter. Our complete code to do this is:
function Update () {
if (Input.GetButtonDown("Jump"))
{
var newTarget = GameObject.FindWithTag("Cube").transform;
GetComponent(Follow).target = newTarget;
}
}