---
title: "Lesson 2.4 - Collision Decisions"
content_type: tutorial
url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/lesson-2-4-collision-decisions?language=en"
language: en
container_type: course
container_title: "Unit 2 - Basic Gameplay"
container_url: "https://learn.unity.com/course/unit-2-basic-gameplay.md?language=en&version=6.0"
standalone_markdown_url: "https://learn.unity.com/tutorial/lesson-2-4-collision-decisions.md?language=en&version=6.0"
previous_title: "Lesson 2.3 - Random Animal Stampede"
previous_url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/lesson-2-3-random-animal-stampede?language=en&version=6.0"
previous_markdown_url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/lesson-2-3-random-animal-stampede.md?language=en&version=6.0"
next_title: "Challenge 2 - Play Fetch"
next_url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/challenge-2-play-fetch?language=en&version=6.0"
next_markdown_url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/challenge-2-play-fetch.md?language=en&version=6.0"
difficulty: beginner
duration_minutes: 50
unity_version:
  - "6.0"
  - "6.3"
supported_unity_versions:
  - name: "6.0"
    url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/lesson-2-4-collision-decisions.md?language=en&version=6.0"
  - name: "6.3"
    url: "https://learn.unity.com/tutorial/lesson-2-4-collision-decisions.md?language=en&version=6.3"
  - name: "2022.3"
    url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/lesson-2-4-collision-decisions.md?language=en&version=2022.3"
  - name: "2021.3"
    url: "https://learn.unity.com/course/unit-2-basic-gameplay/tutorial/lesson-2-4-collision-decisions.md?language=en&version=2021.3"
last_updated: "2026-08-04T14:24:52.000Z"
---

# Lesson 2.4 - Collision Decisions

Implement timed animal spawning, collision-based destruction, and a Game Over state in your scene.

**Overview:**

Our game is coming along nicely, but there are are some critical things we must add before it’s finished. First off, instead of pressing S to spawn the animals, we will spawn them on a timer so that they appear every few seconds. Next we will add colliders to all of our prefabs and make it so launching a projectile into an animal will destroy it. Finally, we will display a “Game Over” message if any animals make it past the player.

**Project Outcome:**

The animals will spawn on a timed interval and walk down the screen, triggering a “Game Over” message if they make it past the player. If the player hits them with a projectile to feed them, they will be destroyed.

Overview: [Video](https://connect-mediagw.unity.com/h1/20241208/videos/9a870712-c5ff-4cc7-912c-5dc8cb7aa3f3_CWC_U2.L4_Intro._U6.mp4) ([Transcript (en)](https://connect-mediagw.unity.com/h1/20241208/667b1e2e-a148-4cad-921e-69b7d4ed5ac5_CWC_U2.L4_Intro._U6.mp4.vtt))

## 1. Make a new method to spawn animals

Our Spawn Manager is looking good, but we’re still pressing S to make it work! If we want the game to spawn animals automatically, we need to start by writing our very first custom function.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLqcB%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnLqi1%22%5D.overviewVideo/Unit%202.4%20-%20Step%201%20-%20Make%20a%20new%20method%20to%20spawn%20animals%20-%20FINAL%20RENDER_20260622_193247.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLqcB%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnLqi1%22%5D.subtitles/Unit%202.4%20-%20Step%201%20-%20Make%20a%20new%20method%20to%20spawn%20animals%20-%20FINAL%20RENDER_20260622_193247_20260623_004115.vtt))

1. In **SpawnManager.cs**, create a new **_void SpawnRandomAnimal() {}_** function beneath **_Update()_**
2. Cut and paste the code from the **if-then statement** to the **new function**
3. Call **_SpawnRandomAnimal();_** if **S** is pressed

![](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLqcB%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnLrVe%22%5D.image/1.1-Make%20a%20new%20method%20to%20spawn%20animals_20260420_210427.png)

## 2. Spawn the animals at timed intervals

We’ve stored the spawn code in a custom function, but we’re still pressing S! We need to spawn the animals on a timer, so they randomly appear every few seconds.

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLrYZ%22%5D.body%5B_key==%220aab8275b7c2%22%5D.overviewVideo/Unit%202.4%20-%20Step%202-%20Spawn%20the%20animals%20at%20timed%20intervals%20-%20FINAL%20RENDER_20260622_193754.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLrYZ%22%5D.body%5B_key==%220aab8275b7c2%22%5D.subtitles/Unit%202.4%20-%20Step%202-%20Spawn%20the%20animals%20at%20timed%20intervals%20-%20FINAL%20RENDER_20260622_193754_20260623_004126.vtt))

1. In **_Start()_**, use **_InvokeRepeating_** to spawn the animals based on an interval, then **test**.
2. Remove the **if-then statement** that tests for **S** being pressed
3. Declare new **_private startDelay_** and **_spawnInterval_** variables then playtest and tweak variable values

![](https://connect-mediagw.unity.com/h1/20190522/learn/images/ea0a808c-d2e7-4a8f-b0f8-8e2b66ad05b8_2_4_2.png)

## 3. Add collider and trigger components

_Animals spawn perfectly and the player can fire projectiles at them, but nothing happens when the two collide! If we want the projectiles and animals to be destroyed on collision, we need to give them some familiar components - “colliders.”_

[Video](https://connect-mediagw.unity.com/h1/20241208/videos/26fa7e18-b14a-47f7-8051-7f0b450ba11b_CWC_U2.L4.S3_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLsan%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnLsgd%22%5D.subtitles/CWC_U2.L4.S3_20260505_042613.srt))

1. Double-click on one of the **animal prefabs**, then _Add Component > Box Collider_.
2. Make sure “Auto Save” is enabled in the upper right corner of the **Scene** view_._
3. Click **Edit Collider**, then **drag** the collider handles to encompass the object.
4. Check the “**Is Trigger**” checkbox.
5. If you edited the animal prefab outside the Prefab Editing Mode, use the **Overrides** dropdown to apply this Trigger to all of the prefabs for this animal.
6. Repeat this process for each of the **animals** and the **projectile**.
7. Add a **RigidBody component** to the projectile and uncheck “use gravity”.

## 4. Destroy objects on collision

_Now that the animals and the projectile have Box Colliders with triggers, we need to code a new script in order to destroy them on impact._

[Video](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLu6D%22%5D.body%5B_key==%22a0903531f1e6%22%5D.overviewVideo/CWC_U2_L4_S4_U6_V2_20251222_231746.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLu6D%22%5D.body%5B_key==%22a0903531f1e6%22%5D.subtitles/CWC_U2_L4_S4_U6_V2.mp4_20251222_231822.srt))

1. Create a new **DetectCollisions.cs** script, add it to each animal prefab, then **open** it
2. Before the final **}** add **_OnTriggerEnter_** function using **autocomplete**
3. In **_OnTriggerEnter_**, put **_Destroy(gameObject)_;**, then test
4. In **_OnTriggerEnter_**, put **_Destroy(other.gameObject);_**

![](https://connect-mediagw.unity.com/h1/20190522/learn/images/f964ea7d-eeeb-4b49-903c-309bc119287f_2_4_4.png)

## 5. Trigger a “Game Over” message

The player can defend their field against animals for as long as they wish,  but we should let them know when they’ve lost with a “Game Over” message if any animals get past the player.

[Video](https://connect-mediagw.unity.com/h1/20241208/videos/dfa2f0c5-5d11-48bb-858a-35d5688704b1_CWC_U2.L4.S5_U6.mp4) ([Transcript (en)](https://storage.googleapis.com/learn-platform-bucket-production/tutorial/f1f2031f-fd9d-4698-a35d-8d0e3c102ef6/versions%5B_key==%2286a7de8aeae2%22%5D.sections%5B_key==%22rbCl1Be6HAVyj2JQSnLvK7%22%5D.body%5B_key==%22rbCl1Be6HAVyj2JQSnLvPx%22%5D.subtitles/CWC_U2.L4.S5_20260505_042637.srt))

1. In DestroyOutOfBounds.cs, in the **else-if condition** that checks if the animals reach the bottom of the screen, add a Game Over message:
   **_Debug.Log(“Game Over!”)_**
2. Clean up your code with **comments**
3. If using Visual Studio, Click _Edit > Advanced > **Format document**_ to fix any indentation issues
   (On a **Mac**, click _Edit > Format > Format Document)_

![](https://connect-mediagw.unity.com/h1/20190522/learn/images/b80ab1fa-3fd5-439f-aa48-9af139b0dac0_2_4_5.png)

## 6. Lesson Recap

## New Functionality

- Animals spawn on a timed interval and walk down the screen
- When animals get past the player, it triggers a “Game Over” message
- If a projectile collides with an animal, both objects are removed

## New Concepts & Skills

- Create custom methods/functions
- InvokeRepeating() to repeat code
- Colliders and Triggers
- Override functions
- Log Debug messages to console

## Check your scrip

SpawnManager.cs

```csharp
using UnityEngine;
using UnityEngine.InputSystem;

public class SpawnManager : MonoBehaviour
{
    public GameObject[] animalPrefabs;
    public InputAction spawnAction;

    private float spawnRangeX = 20;
    private float spawnPosZ = 20;
    private float startDelay = 2;
    private float spawnInterval = 1.5f;

    void Start()
    {
        spawnAction.Enable();
        InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);
    }

    void Update()
    {
        
    }

    void SpawnRandomAnimal() 
    {
        int animalIndex = Random.Range(0, animalPrefabs.Length);
        Vector3 spawnpos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 0, spawnPosZ);
        Instantiate(animalPrefabs[animalIndex], spawnpos, animalPrefabs[animalIndex].transform.rotation);
    }
}
```

DetectCollisions.cs

```csharp
using UnityEngine;

public class DetectCollisions : MonoBehaviour
{
    void Start()
    {
        
    }

    void Update()
    {
        
    }

    void OnTriggerEnter(Collider other)
    {
        Destroy(gameObject);
        Destroy(other.gameObject);
    }
}
```

DestroyOutOfBounds.cs

```csharp
using UnityEngine;

public class DestroyOutOfBounds : MonoBehaviour
{
    private float topBound = 30;
    private float lowerBound = -10;

    void Start()
    {
        
    }

    void Update()
    {
        // If an object goes past the players view in the game, remove that object.
        if (transform.position.z > topBound)
        {
            Destroy(gameObject);
        }
        else if (transform.position.z < lowerBound)
        {
            Debug.Log("Game Over!");
            Destroy(gameObject);
        }
    }
}
```
