# Accessing Components Through Scripts

During gameplay, you won't be able to click and drag on objects to update them, so here are some ways to access common properties through scripts.

## Declaring Variables

In your script, public variables can be declared at the top of the class before the Start() function.

Example:

```csharp
public GameObject objectToAppear;
public Transform objectToMove;
public Rigidbody rb;
```

Once saved and added to a game object, the example will appear like this:

![](https://3795241431-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8XSUYVSlHMdPUNud-2%2F-MEoHr294fvSPD3QRldm%2F-MEoS1AwxE2tMB8Q-aTi%2Fimage.png?alt=media\&token=9712366b-24c1-4d65-85a9-315fa8ea7a11)

This means the script is looking for specific types of components (GameObject, Transform, and Rigidbody) and took the scripts and turned the variable name into something more readable (if the name is written in "camel" case).

## Assigning Variables

### **Using the Inspector**

Click and drag the game objects you want to manipulate with the script into the proper fields OR clock on the circle/target icon to the right of the field to search for and select the game object. Clicking the target icon will only list objects that have the type of component required.

![](https://3795241431-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8XSUYVSlHMdPUNud-2%2F-MEoHr294fvSPD3QRldm%2F-MEoRj1yYJok73tHR-Yi%2FAddingObjectToScripts_01.gif?alt=media\&token=990f521d-d704-43a7-934e-7e2632223f7d)

Once filled in, it will look similar to this:

![](https://3795241431-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8XSUYVSlHMdPUNud-2%2F-MEoHr294fvSPD3QRldm%2F-MEoRvYt9urdPnmLoit4%2Fimage.png?alt=media\&token=167605aa-373f-401f-9498-03861d786233)

### **Using a Script**

There are times when you need to get a component during gameplay that has changed or has been added that you can't click and drag into an editor field.

There is a GetComponent<> function that can be used to get one component and a GetComponent**s**<> function that gives you an array of the type of components in multiple objects.

Put the type you are looking for within the brackets <>.

This example shows an example of declaring and assigning a private/local Rigidbody variable using GetComponent<>:

```csharp
Rigidbody rb = objectToMove.GetComponent<Rigidbody>();
```

This tells the script to take the "objectToMove", find the Rigidbody component, and store it as "rb".

{% hint style="warning" %}
If there is a reference an object in the script and the Inspector field is empty and the script doesn't assign the variable, you will get a common error:

*UnassignedReferenceException: The variable \_\_\_\_\_ of \_\_\_\_\_ has not been assigned. You probably need to assign the \_\_\_\_\_ variable of the \_\_\_\_\_ script in the inspector.*
{% endhint %}

## Updating Values

So now, the variable "rb" can be used with any Rigidbody functions.

Note: Storing information into variables is optional. The GetComponent<> function can also access the functions for the type within the brackets <>.

Both lines of code do the same thing:

```csharp
rb.useGravity = false;
objectToMove.GetComponent<Rigidbody>().useGravity = false;
```

This guide will not go in-depth in most components, so please search the Unity Documentation for details.

Generally, you can start typing the name of the value you would like to update and something close will appear in the smart text feature in your IDE/code editor. It will often give you details such as what the command does, what type uses it, what kind of value it uses, and what information is needed to work. There will be arrows that you can scroll through if there are multiple uses of the same function.

![](https://3795241431-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M8XSUYVSlHMdPUNud-2%2F-M9LlgHYuaHQ5o1mfgph%2F-M9Lv-8C4b0A2IhB9yVo%2Fimage.png?alt=media\&token=b5e2e5ef-90fc-4602-9420-2ebca706f194)

{% content-ref url="../../delete/delete-game-objects/destroy-function" %}
[destroy-function](https://hopemoore.gitbook.io/unity-basics/delete/delete-game-objects/destroy-function)
{% endcontent-ref %}

{% content-ref url="../../create/create-scripts/custom-components-and-scripts" %}
[custom-components-and-scripts](https://hopemoore.gitbook.io/unity-basics/create/create-scripts/custom-components-and-scripts)
{% endcontent-ref %}

## More Info

Some components are covered in these other sections:

{% content-ref url="../../materials" %}
[materials](https://hopemoore.gitbook.io/unity-basics/materials)
{% endcontent-ref %}

{% content-ref url="../../interaction" %}
[interaction](https://hopemoore.gitbook.io/unity-basics/interaction)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hopemoore.gitbook.io/unity-basics/select/components/accessing-attributes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
