Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Default Script
  • Start() Function
  • Update() Function
  • Adding Custom Code

Was this helpful?

  1. Coding Basics

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:

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

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

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:

PreviousMouse ControlsNextVariables

Last updated 4 years ago

Was this helpful?

You can store values, messages, and objects into variables, then control and update them through code. variables will appear in the window when the script is added to a game object.

Variables
Conditionals (If / Then / Else)
Arrays
Loops
Functions
Inspector
Public