# Intro to Scripts

Scripts are a way to create custom components that control the properties and interaction with game objects. Unity uses C# plus its own library of code so developers don't have to type everything from scratch.

## Default Script

Let's take a look at what is in a default script:

```csharp
using UnityEngine;

public class Example2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
```

**using UnityEngine** - this is one of the libraries that hold code making accessing game objects and components easier. Do not remove this!

**public class** - tells the program this script is accessible by other scrips and the code within this .cs file is a "class" ... think of it as saying this script is a full component that can be added and removed from game objects.

**Example2** - the name of the script. This shows as "Example2.cs" in the Assets folder and "Example 2" in the Inspector.

**: MonoBehaviour** - built-in parent class that tells the program how to handle the component. All the code for the component should exist inside the brackets that follow.

## Start() **Function**

Unity provides a helpful comment here. The code within the curly brackets after Start() runs **only once** and on the **first frame**.

This is similar to the setup() function s found in some other programming languages.

## Update() Function

Just as the comment says, the code within the curly brackets after Update() will run **once per frame** - for many computers, that could mean 60 times a second!

This is similar to loop() or draw() in some other programming languages.

## Adding Custom Code

You can store values, messages, and objects into variables, then control and update them through code. [**Public**](https://hopemoore.gitbook.io/unity-basics/variables#public-vs-private-variables) variables will appear in the [Inspector](https://hopemoore.gitbook.io/unity-basics/the-unity-interface/the-tabs/inspector-tab) window when the script is added to a game object.

The biggest thing to remember when accessing parts of your game and game objects is that it starts with the biggest element and gets smaller with dots in between.

**Examples:**

Game Object > Component > Property > Value/Function

```csharp
gameObject.GetComponent<Renderer>().material.color = new Color(1.0F, 0, 0, 1.0F);

gameObject.GetComponent<Rigidbody>().iskinematic = false;

print(gameObject.name);
```

Please see the following sections for more information about the elements used in coding:

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

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

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

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

{% content-ref url="functions" %}
[functions](https://hopemoore.gitbook.io/unity-basics/coding-basics/functions)
{% 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/coding-basics/intro-to-scripts.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.
