# Raycasting (Unity)

This activity uses Unity 2020.2 and all code should allow you to copy and paste.

## Raycasting

Raycasting is a powerful way (just like collisions) to get and update information about an object in Unity.

This activity is a VERY simple example, but this method of having Raycasting from the camera is used in first-person video games and experiences. Rays can be cast from objects as well (aiming weapons/tools, having code run when an NPC (non-player character) is looking at an object, etc.)

## Starter Scripts

To save time, please download these scripts to help with the movement of the camera.

{% file src="https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPIzBEZC02WnW0XUtM%2FfirstPersonCameraControls.zip?alt=media&token=4dae7649-91ac-4197-9270-fb39ee4e6a92" %}
firstPersonCameraControls.zip
{% endfile %}

Inside this file:

**PlayerMovement.cs** - Uses WASD controls to move in directions relative to the object it is attached to. Can be used with other player objects as well.

**RotateCamera.cs** - A script I found years ago and have since modified that uses your mouse to look around. It might not be the most efficient, but it's good for testing other scripts. I also added a code for making the cursor hidden since the rotation will not match and this throws off the user. This, however, doesn't seem to work within Unity for Unity 2020.2 and newer.

## Step 1: Create and Set Up a New Scene

Open a previous Unity project file or create a new one.

{% hint style="info" %}
The layout in these examples uses a custom layout so both the Scene and Game windows can be viewed at the same time as well as having the Console visible. You can move the tabs and windows around by clicking and dragging the tabs.
{% endhint %}

### Create a New Scene

Create a new scene in your Project window/tab by going to the default **Scenes** folder, then right-click and choose Create > Scene.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPJj4quTU6UGVRo8l6%2Fimage.png?alt=media\&token=c61f4d97-9fef-497c-ac72-8fbc3a9ae43a)

Name your scene. I named this one "Looking at Things."

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPJooX9bn4HQ20rwVW%2Fimage.png?alt=media\&token=47ca985d-c096-47d1-9d41-6ef504a21652)

Double-click on the new scene. This will open the scene in the Hierarchy, Scene, and Game windows.

The name of the scene will be at the top of the Hierarchy window. A scene will have a Main Camera and Directional Light by default.&#x20;

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPJtfmGOAyb7K1iQcr%2Fimage.png?alt=media\&token=4bab0e81-c5cb-44fe-8a46-473cf8539677)

### Add the Starter Codes

Once you have downloaded and unpacked/extracted the files from the zip files, click and drag them into your Assets folder or a Scripts subfolder in the **Project** window. This will make copies in your Unity project.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPKb0kt_VC6e46TrAz%2Fimage.png?alt=media\&token=a2f65d7c-e152-43ff-befe-ebfff36562ad)

## Step 2: Add Starter Scripts to Your Camera

You can:

**Option 1:** Click and drag the scripts onto the Main Camera object in the Hierarchy window

**Option 2:** Select the Main Camera in the Hierarchy window, bringing up the information in the Inspector window. Click and drag the scripts into the Inspector window

**Option 3:** Select the Main Camera in the Hierarchy window, bringing up the information in the Inspector window. Click the **Add Component** button and search for the scripts to add them.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPKYxj2wcKUd604QC1%2Fimage.png?alt=media\&token=ef3eac1a-6d23-42ad-85d4-f657af6adb78)

When added correctly, the scripts will appear as components:

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVOVaeKhzy4ikOMMUfF%2F-MVPKqWuCs49Z0gJeWy5%2Fimage.png?alt=media\&token=11646d36-deb9-45c3-a1a3-111c95f7962a)

You can test the scene by pressing play, but it won't look like much without anything in the scene to show relative distance.

## Step 3: Add Objects

Objects here are just examples to test our Raycasting. When developing projects, simple things are used to test code before using the script in more detailed environments.

I will use a sphere and a cube, but you can use any other primitive 3D shape.

To add a 3D primitive shape object, right-click in the Hierarchy window and select 3D Object and one of the first five shapes.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPL-yrpyUmUEzahPND%2Fimage.png?alt=media\&token=e678b868-850e-419a-b266-b6150a6d3165)

Reset the location of your objects by selecting them and right-clicking on the Transform component in the Inspector window and select **Reset**.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPL_Iesd4UGINf1tEY%2Fimage.png?alt=media\&token=d3c64c88-98b3-4e62-ad3f-b526de465c75)

Resetting the transform will put the objects at (0, 0, 0).

Rename your objects by right-clicking and selecting Rename.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPLpe6ogcLO9L0GHM8%2Fimage.png?alt=media\&token=cde23713-a0a9-41c0-b7fd-6064ad300477)

Move your objects with the **Move Tool**. Press W for a shortcut or click on the Move Tool button on the top left:

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPLvyZaHXvmuLaSbSO%2Fimage.png?alt=media\&token=9b346d09-fc90-42a3-846b-51997027a64b)

You will want to move them closer to the camera and along the same horizon.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPMKtUQJo726B8BBoA%2Fimage.png?alt=media\&token=44460505-28d8-4fa7-9dde-99426b323371)

{% hint style="info" %}
Your Scene window might be looking at your scene from a different angle than your Game window. The quickest way to get it to be the same direction, click on the red X end of the gizmo on the top right of your screen to make the cone get bigger on the right (as pictured).
{% endhint %}

You can test the scene now and it is best when viewed with **Maximize On Play** is on. Press ESC to bring the mouse back to stop playing.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPTrPKOVgUjYxV0xR3%2Fraycasting-01.gif?alt=media\&token=7d4555c5-3798-48cd-9dc8-843e0dc536d7)

## Step 4: Add a User Interface

One form of a user interface appears as a dot in the center of the screen that changes when the ray hits something, often with an added prompt (what will happen when the player presses a button).

We can do this with UI (User Interface) objects.

Start with a **Canvas** by right-clicking and selecting UI > Canvas.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPUAhaQglQBccu1XPs%2Fimage.png?alt=media\&token=c62ff1f0-b2c8-4ae8-8453-a56405edbc05)

This will also load an EventSystem.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPUIrLnuR3ZpeI5xO9%2Fimage.png?alt=media\&token=9fd53691-f0a8-4e60-bc95-6aec65b8d594)

By default, the Render Mode is in Screen Space - Overlay so what is on this canvas appears as an overlay of the entire screen. However, in the Scene window, this canvas looks HUGE.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPV5Y0udgX_2eRDY-H%2Fraycasting-02.gif?alt=media\&token=2cb19c0e-012e-45ce-9a48-846df5c7a14d)

The good news is that the elements can be positioned with the move tool, making it easy to move them.

Add a **Panel** object. (Create > Panel)

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPVGg-PFcPyrGk6bai%2Fimage.png?alt=media\&token=56280783-50e6-4d1e-8201-a159fc6954c4)

This should create it as a child object of the Canvas object:

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPVOIFmNslZr8ymgVq%2Fimage.png?alt=media\&token=ad4a29bf-de8c-4263-b204-4b3a1d271fe1)

The Source Image of the Panel is background by default and appears as a gray overlay.

Select the Panel object. In the Inspector window, you'll see that the Source Image of the Panel is background by default and appears as a gray overlay.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPVj5HTfLd6EKpA6ok%2Fimage.png?alt=media\&token=408e27e8-1eff-41b6-839c-734a7bacfd14)

This changes the image to a circle. However, it appears as an ellipse because it's trying to match the screen's aspect ratio.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPWFudI0ZlvE9jTHEn%2Fimage.png?alt=media\&token=0fe7f7a3-420e-4be2-bfa2-106510764286)

In the Inspector window, select **Preserve Aspect** under _Image Type_ so that it is checked.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPWgwg-lGIL_lodaP1%2Fimage.png?alt=media\&token=659ff28b-9303-401f-b776-3d515a4d9817)

This will make the circle keep its shape in differently sized screens.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPWsQV-BWrph8xl2Bj%2Fimage.png?alt=media\&token=447de583-53a4-4522-b6d4-dbd09fecb09e)

Use the Scale Tool to make the circle SUPER tiny - almost dot-like.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPX25KZ_urtqtcka0X%2Fimage.png?alt=media\&token=71a4e731-af1a-4dc5-986a-1d1649694943)

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPX898jo-DBT80E5s5%2Fimage.png?alt=media\&token=c4c80837-0124-4c12-87a6-9adcf5f9ac25)

Size of my dot in the Scene window (color change to gray so you can see the size):

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPXdDUj73GmfPL0oUo%2Fimage.png?alt=media\&token=11373c8a-a252-42bd-a90c-1271a2fb1c17)

Select the Panel in the Hierarchy window and duplicate (right-click > Duplicate or CTRL+D) the object.

Select the new Panel (1) and use the Scale Tool to make the dot slightly, but noticeably larger.

![Color changed to gray to be visible.](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPXoJCEbBUFMTn1Txl%2Fimage.png?alt=media\&token=3ceff82d-8cca-47e2-a273-dab5fe6ff83b)

Rename your Panel object to Default Dot and Panel (1) to Interact Dot.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPY3PyWjm7iBvr6RDy%2Fimage.png?alt=media\&token=17bc9c30-eaa4-4d27-8808-3bb8c044f06a)

Add a Text UI object (Right-click > UI > Text).

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPY9QKN0z2QuRQQkQy%2Fimage.png?alt=media\&token=3dab3489-6a0d-4043-bc81-8a8cc2e3df41)

Rename it to Prompt Text.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPYF782jxe7E6gs9xz%2Fimage.png?alt=media\&token=7292b92a-4197-4c26-b80c-c4783652fbbb)

The default text says, "New Text" and will place a text object in the center of the screen. It will appear off to the left because it is left-aligned.

In the Inspector window in the **Text** component:

* Change the **alignment** under _Paragraph_ to Center-Center
* Change "New Text" to "Interact"
* Change the color to something more noticeable than Dark Gray.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPYfaTPNetixczlz--%2Fimage.png?alt=media\&token=1ef8f871-522c-482b-b5b9-7246eae0fecf)

Use the Move Tool to move the text down off of the dots.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPYkY2zbYU7Ed_SJ4m%2Fimage.png?alt=media\&token=c1a13a2b-6770-40cd-8fea-4b7ab2792b50)

![The text is just under the dots.](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPYtk6Wh4WvIBq8zaF%2Fimage.png?alt=media\&token=fe1cf5b5-b589-4cfa-a109-2716fd1ebe67)

## Step 5: Creating a Raycasting Script

Now, we'll write a script that sends an invisible "ray" or line from one position, in a direction and get information about whatever it hits.

Create a new C# script by right-clicking in the Project window and selecting Create > C# Script.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPZ48jrkpPyyf5DGLS%2Fimage.png?alt=media\&token=83cff5ad-9605-4553-b6f0-501d147207ef)

Name it something like RaycastingTest.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVPZA3R358ow_nYgt-M%2Fimage.png?alt=media\&token=cb52000b-b952-47e1-a930-b34514a02e7f)

Double-click the file to open it in Visual Studio.

We will be accessing and updating UI elements, so we need to include the library for `UI`. Add this to the top of the script immediately after using `UnityEngine;` and before the code for the class.

```csharp
using UnityEngine.UI;
```

We want the UI to change if our ray hits something so create variables for:

* default dot (GameObject)
* interact dot (GameObject)
* prompt text object (GameObject)
* prompt text message (Text)

```csharp
public GameObject defaultDot;
public GameObject interactDot;
public GameObject promptTextObject;
public Text promptText;
```

In the `Update()` function, create a variable for the object that is hit's information.&#x20;

We want to put it in an if statement to have things happen ONLY when the ray hits something.

```csharp
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out RaycastHit hit, 5))
{

}
```

About this line of code:

* Raycasting is part of the Physics class that controls all the physics calculations and functions
* `Physics.Raycast()` can take different numbers of arguments to help determine the position, direction, distance, layer, etc. Details appear once you start typing the beginning parenthesis in Visual Studio.
* `out` is a way of putting information into a variable - we'll call it `hit`.
* The Transform component will have a built-in function that can determine a direction from the object the transform component is on

The function used here is `Physics.Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance)`

The Ray includes the beginning position and direction of the ray.

{% hint style="info" %}
You'll notice I didn't have `== true` in the if statement test. It is not needed since it is testing for true or false and having `== true` is like asking if `true == true`.
{% endhint %}

Have the dots switch to the larger dot when true and smaller dot when false. Also, have the prompt only when the ray is hitting something. We can use `GameObject.SetActive()` to toggle the availability of a game object.

```csharp
void Update()
{

    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out RaycastHit hit, 5))
    {
        defaultDot.SetActive(false);    // Turns off Default Dot
        interactDot.SetActive(true);    // Turns on Interact Dot
        promptTextObject.SetActive(true);   // Turns on the prompt text object
    }
    else
    {
        defaultDot.SetActive(true);    // Turns on Default Dot
        interactDot.SetActive(false);    // Turns off Interact Dot
        promptTextObject.SetActive(false);   // Turns off the prompt text object
    }
}
```

Save your script and return to Unity.

Add your RaycastingTest script to your **Main Camera** object. (click and drag or use the **Add Component** button)

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVP_pFi8f1lG4xSt_2U%2Fimage.png?alt=media\&token=5a91bec7-8c26-4951-8a63-e187be96fd37)

The new component will appear like this when Main Camera is selected:

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVP_dieQereS2UxQjnJ%2Fimage.png?alt=media\&token=89cfdc13-eedc-4909-bbba-af56908115b4)

Update these fields using the click and drag method (from Hierarchy to the fields in Inspector) or click on the target/bull's eye icon for each. **Prompt Text** will be the object for both Prompt Text Object and Prompt Text.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPKuqIFLma9e58VdwB%2F-MVP_zo0V1X0ECxgIKNE%2Fimage.png?alt=media\&token=56f255a9-c606-404f-b424-1cba53a3f1e2)

Now, test it and see if the dot changes and the text appears when close enough to an object and looking at it.

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPa1VkIKsJbZ2prgW7%2F-MVPbgCq56uoNFVOTtFT%2Fraycasting-03.gif?alt=media\&token=fcca3086-c83c-41a2-a9fb-b35e6ad3507c)

## Step 6: Affecting Individual Objects

Let's make each object act independently. There will be times when you will need to change things the ray is hitting instead of all objects. You might not have the information to store and control until after the program is running. The ray will get the information it is interacting with/hitting.

I want to make the object I'm looking at get bigger when when I click the left mouse button and smaller with the right.

### Adding Interaction

Let's use if statements to check for button presses.

I'll use `Input.GetKeyDown(KeyCode key)` to run code the frame the key is first pressed. To run the code again, I'll need to release the key and press it again. I don't know the key code of the letters, so I access it by going through the KeyCode library.

We can access the object we are hitting by getting the object stored in the variable `hit` and accessing its Transform component.

```csharp
if (Input.GetKeyDown(KeyCode.B))
{
    // Make hit object larger by 125 percent
    hit.transform.localScale *= 1.25F;
}

if (Input.GetKeyDown(KeyCode.N))
{
    // Make hit object smaller by 75 percent
    hit.transform.localScale *= 0.75F;
}
```

Full code so far:

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RaycastingTest : MonoBehaviour
{
    public GameObject defaultDot;
    public GameObject interactDot;
    public GameObject promptTextObject;
    public Text promptText;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out RaycastHit hit, 5))
        {
            defaultDot.SetActive(false);    // Turns off Default Dot
            interactDot.SetActive(true);    // Turns on Interact Dot
            promptTextObject.SetActive(true);   // Turns on the prompt text object
        }
        else
        {
            defaultDot.SetActive(true);    // Turns on Default Dot
            interactDot.SetActive(false);    // Turns off Interact Dot
            promptTextObject.SetActive(false);   // Turns off the prompt text object
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            // Make hit object larger by 125 percent
            hit.transform.localScale *= 1.25F;
        }

        if (Input.GetKeyDown(KeyCode.N))
        {
            // Make hit object smaller by 75 percent
            hit.transform.localScale *= 0.75F;
        }

    }
}

```

Save your script and test it in Unity!

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPeiO843Fp87wBdYpm%2F-MVPfGD-Ecw2uhhDfpQl%2Fraycasting-04.gif?alt=media\&token=13a1a97d-7913-45c7-8429-58aa2f703d2b)

## Step 7: Updating the UI Text

Go back to the script in Visual Studio.

We should have something more descriptive than "Interact," so let's change it in the raycasting if statement.

Update the prompt text to say, "Change the size of " and the name of the object. You can get it through the transform:

```csharp
promptText.text = "Change size of " + hit.transform.name;  // Updates the text to match the name of the object
```

Save your script and test it!

![](https://3674232557-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MR1EQixIaV64lGrdh2s%2F-MVPeiO843Fp87wBdYpm%2F-MVPhjs9DQSS6zR5vP2B%2Fraycasting-05.gif?alt=media\&token=e1aa548a-c6e5-477f-b7af-912c9277c2f9)

Just think of what you could do with tags (grouping items) and running code on specific instances of objects!

## Full Code

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RaycastingTest : MonoBehaviour
{
    public GameObject defaultDot;
    public GameObject interactDot;
    public GameObject promptTextObject;
    public Text promptText;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out RaycastHit hit, 5))
        {
            defaultDot.SetActive(false);    // Turns off Default Dot
            interactDot.SetActive(true);    // Turns on Interact Dot
            promptTextObject.SetActive(true);   // Turns on the prompt text object
            promptText.text = "Change size of " + hit.transform.name;  // Updates the text to match the name of the object
        }
        else
        {
            defaultDot.SetActive(true);    // Turns on Default Dot
            interactDot.SetActive(false);    // Turns off Interact Dot
            promptTextObject.SetActive(false);   // Turns off the prompt text object
        }

        if (Input.GetKeyDown(KeyCode.B))
        {
            // Make hit object larger by 125 percent
            hit.transform.localScale *= 1.25F;
        }

        if (Input.GetKeyDown(KeyCode.N))
        {
            // Make hit object smaller by 75 percent
            hit.transform.localScale *= 0.75F;
        }

    }
}

```

Save your script and test it!
