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
  • Declaring Variables
  • Assigning Variables
  • Using the Inspector
  • Using a Script
  • Updating Values
  • More Info

Was this helpful?

  1. Select and Update
  2. Updating Components

Accessing Components Through Scripts

PreviousUpdate Components in the InspectorNextDeleting Game Objects

Last updated 4 years ago

Was this helpful?

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:

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

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

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.

Once filled in, it will look similar to this:

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 GetComponents<> 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<>:

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

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

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.

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:

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.

More Info

Some components are covered in these other sections:

Using the Destroy() Function
Creating Custom Components and Scripts
Materials
Interaction