<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alok Krishali</title>
    <description>The latest articles on DEV Community by Alok Krishali (@alok_krishali).</description>
    <link>https://dev.to/alok_krishali</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2912591%2Fbfe972b5-4365-4c02-b1f1-b2cf6f152bd0.jpg</url>
      <title>DEV Community: Alok Krishali</title>
      <link>https://dev.to/alok_krishali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alok_krishali"/>
    <language>en</language>
    <item>
      <title>How to Create a Smooth Camera Follow in Unity Using Cinemachine</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Thu, 03 Dec 2026 01:26:00 +0000</pubDate>
      <link>https://dev.to/alok_krishali/how-to-create-a-smooth-camera-follow-in-unity-using-cinemachine-52n0</link>
      <guid>https://dev.to/alok_krishali/how-to-create-a-smooth-camera-follow-in-unity-using-cinemachine-52n0</guid>
      <description>&lt;p&gt;Have you ever played a game where the camera follows the player so smoothly that you barely notice it moving? A good camera system can make a big difference in how a game feels. On the other hand, a camera that moves too quickly, shakes unnecessarily, or struggles to keep up with the player can make even a good game feel uncomfortable.&lt;/p&gt;

&lt;p&gt;If you are developing a &lt;strong&gt;&lt;a href="https://unity.com/" rel="noopener noreferrer"&gt;game in Unity&lt;/a&gt;&lt;/strong&gt;, you don't need to build a camera-follow system from scratch. &lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-cinemachine-tutorial/" rel="noopener noreferrer"&gt;Unity Cinemachine&lt;/a&gt;&lt;/strong&gt; provides tools that help you create smooth, responsive, and professional-looking cameras with very little code.&lt;/p&gt;

&lt;p&gt;In this tutorial, you will learn how to set up a camera that follows your player using Cinemachine, adjust its smoothness, and fix some common problems along the way.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Cinemachine in Unity?
&lt;/h2&gt;

&lt;p&gt;Cinemachine is a camera system for Unity that helps developers create better camera movements without writing complex camera scripts.&lt;/p&gt;

&lt;p&gt;Instead of manually updating the camera position every frame, you can configure Cinemachine to follow a target and control how the camera behaves.&lt;/p&gt;

&lt;p&gt;With Cinemachine, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make the camera smoothly follow the player.&lt;/li&gt;
&lt;li&gt;Control the camera's follow distance and offset.&lt;/li&gt;
&lt;li&gt;Adjust how quickly the camera responds to player movement.&lt;/li&gt;
&lt;li&gt;Create different camera behaviours for different gameplay situations.&lt;/li&gt;
&lt;li&gt;Build camera systems for 2D and 3D games.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this tutorial, we will create a basic third-person camera that follows a player in a 3D Unity scene.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create a Unity Project
&lt;/h2&gt;

&lt;p&gt;First, open Unity Hub and create a new 3D project. You can use an existing project if you already have a player character and a scene.&lt;/p&gt;

&lt;p&gt;For a quick test, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new 3D project.&lt;/li&gt;
&lt;li&gt;Add a Plane to the scene to act as the ground.&lt;/li&gt;
&lt;li&gt;Add a Capsule to represent the player.&lt;/li&gt;
&lt;li&gt;Position the Capsule slightly above the Plane.&lt;/li&gt;
&lt;li&gt;Add a simple movement script if you want to test the camera while moving the player.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Make sure your player object has a meaningful name, such as &lt;code&gt;Player&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can use a character model instead of a Capsule later. The Capsule is simply an easy way to test whether your camera is working correctly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Before adjusting camera settings, test the scene with a simple player object. This helps you identify camera problems without worrying about character animations, models, or complicated movement systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Install Cinemachine
&lt;/h2&gt;

&lt;p&gt;Now, let's install Cinemachine in your project.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open Unity.&lt;/li&gt;
&lt;li&gt;Go to &lt;strong&gt;Window &amp;gt; Package Management &amp;gt; Package Manager&lt;/strong&gt;. Depending on your Unity version, you may find Package Manager under the Window menu directly.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Unity Registry&lt;/strong&gt; from the package source dropdown.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Cinemachine&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select the package and click &lt;strong&gt;Install&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Wait for Unity to finish installing the package.&lt;/p&gt;

&lt;p&gt;The exact interface can vary depending on your Unity and Cinemachine versions. This tutorial uses the Cinemachine workflow available in modern Unity versions, particularly Cinemachine 3.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Cinemachine 2 and Cinemachine 3 have some differences in component names and setup. If your Inspector shows different options, check which version is installed before following the remaining steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create a Cinemachine Camera
&lt;/h2&gt;

&lt;p&gt;After installing Cinemachine, create a camera that will follow the player.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select your player object in the Hierarchy.&lt;/li&gt;
&lt;li&gt;Open the GameObject menu.&lt;/li&gt;
&lt;li&gt;Find the Cinemachine camera creation option. In Cinemachine 3, this is commonly available under &lt;strong&gt;Cinemachine &amp;gt; Cinemachine Camera&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create the Cinemachine Camera.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can also create the camera first and assign the player afterward.&lt;/p&gt;

&lt;p&gt;When the camera is created, Unity may configure a Cinemachine Brain on the Main Camera automatically. If it doesn't, select your Main Camera and add the &lt;strong&gt;Cinemachine Brain&lt;/strong&gt; component from the Inspector.&lt;/p&gt;

&lt;p&gt;The Main Camera renders the scene, while the Cinemachine Camera controls the desired camera behaviour through the Brain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Do not create multiple ordinary cameras unless your game needs them. For a basic follow camera, one Main Camera and one Cinemachine Camera are enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Assign the Player as the Follow Target
&lt;/h2&gt;

&lt;p&gt;This is the most important step.&lt;/p&gt;

&lt;p&gt;Select your Cinemachine Camera in the Hierarchy. Look at its Inspector and find the target settings.&lt;/p&gt;

&lt;p&gt;Depending on the Cinemachine version, you may see fields such as &lt;strong&gt;Tracking Target&lt;/strong&gt;, &lt;strong&gt;Follow&lt;/strong&gt;, or other target-related options.&lt;/p&gt;

&lt;p&gt;Drag your Player object into the appropriate tracking or follow target field.&lt;/p&gt;

&lt;p&gt;The camera now knows which object it should track.&lt;/p&gt;

&lt;p&gt;However, simply assigning the target does not guarantee smooth movement. You still need to configure how the camera follows the player.&lt;/p&gt;

&lt;p&gt;For a third-person camera, you generally want the camera to stay behind and slightly above the player rather than sitting directly inside the character.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Adjust the Camera Position and Follow Distance
&lt;/h2&gt;

&lt;p&gt;Now let's position the camera so the player is clearly visible.&lt;/p&gt;

&lt;p&gt;In Cinemachine 3, select your Cinemachine Camera and look for the &lt;strong&gt;Position Control&lt;/strong&gt; settings. For a basic third-person follow camera, you can use a &lt;strong&gt;Third Person Follow&lt;/strong&gt; position control component or another suitable follow setup, depending on your project.&lt;/p&gt;

&lt;p&gt;A common starting point is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Camera distance: 5 units.&lt;/li&gt;
&lt;li&gt;Camera height: 2 units.&lt;/li&gt;
&lt;li&gt;Camera side offset: 0 units.&lt;/li&gt;
&lt;li&gt;Vertical framing: Keep the player near the centre of the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The exact values depend on your character's size and the type of game you are making.&lt;/p&gt;

&lt;p&gt;If your Cinemachine Camera uses a Third Person Follow component, adjust its camera distance, vertical arm length, and camera side settings to get the desired view.&lt;/p&gt;

&lt;p&gt;If you are creating a top-down game, a third-person camera setup may not be appropriate. Choose a camera configuration that matches your gameplay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Think about the player's surroundings, not just the character. The camera should show enough of the environment for players to see obstacles, enemies, and upcoming paths.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Make the Camera Follow Smoothly
&lt;/h2&gt;

&lt;p&gt;Now comes the part that makes the camera feel better.&lt;/p&gt;

&lt;p&gt;Select your Cinemachine Camera and find the settings related to damping or follow smoothing.&lt;/p&gt;

&lt;p&gt;Depending on your selected position control, these options may appear as damping values, a damping vector, or a similar setting.&lt;/p&gt;

&lt;p&gt;Damping controls how gradually the camera responds when the target moves.&lt;/p&gt;

&lt;p&gt;Try starting with moderate damping values, such as around 0.5 to 1.0 where the selected component exposes values in that range. These are only starting points, not universal settings.&lt;/p&gt;

&lt;p&gt;Test your game and adjust the values based on the result.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens when damping changes?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Very low damping:&lt;/strong&gt; The camera reacts quickly but may feel rigid or abrupt.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Moderate damping:&lt;/strong&gt; The camera follows smoothly while remaining responsive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Very high damping:&lt;/strong&gt; The camera takes longer to catch up and may feel delayed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a fast-paced action game, lower damping may feel better. For a relaxed exploration game, a slower response may create a more cinematic experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Smoothness does not always mean using the highest damping value. Your goal is to make the camera comfortable to control without making it feel disconnected from the player.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Test the Camera with Player Movement
&lt;/h2&gt;

&lt;p&gt;Now play the scene and move your player around.&lt;/p&gt;

&lt;p&gt;Observe the camera while the player moves forward, backward, and sideways.&lt;/p&gt;

&lt;p&gt;Ask yourself these questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the camera keep the player visible?&lt;/li&gt;
&lt;li&gt;Does it move smoothly when the player changes direction?&lt;/li&gt;
&lt;li&gt;Does it take too long to catch up?&lt;/li&gt;
&lt;li&gt;Does the player remain framed correctly during movement?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the camera feels too slow, reduce the damping. If it feels too rigid, increase the damping slightly.&lt;/p&gt;

&lt;p&gt;Make small changes and test again. This is usually the easiest way to find the right settings.&lt;/p&gt;

&lt;p&gt;If your player uses a movement script, make sure the script moves the same GameObject that you assigned as the Cinemachine tracking target.&lt;/p&gt;

&lt;p&gt;Also, check that your player movement and camera setup work together. For example, if the character rotates independently from its movement direction, you may need to adjust the camera's rotation settings separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Add Camera Rotation Controls (Optional)
&lt;/h2&gt;

&lt;p&gt;A third-person camera often needs mouse or controller input so players can look around.&lt;/p&gt;

&lt;p&gt;You can build this behaviour using Cinemachine's rotation controls, input integration, and the Unity Input System.&lt;/p&gt;

&lt;p&gt;The exact setup depends on your Cinemachine version and input configuration. You may need to configure a rotation controller, input axes, or a suitable Cinemachine input component.&lt;/p&gt;

&lt;p&gt;For mouse controls, consider sensitivity and vertical rotation limits. For controller input, consider dead zones and separate horizontal and vertical sensitivity.&lt;/p&gt;

&lt;p&gt;If you only need a camera that follows the player without rotating, you can skip this step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Avoid making the camera rotate too quickly. Sudden rotation can make it difficult for players to judge distances and can create an uncomfortable experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Fix Common Cinemachine Camera Problems
&lt;/h2&gt;

&lt;p&gt;Sometimes the camera does not behave as expected. Here are a few things to check.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 1: The camera does not follow the player
&lt;/h3&gt;

&lt;p&gt;Check that the player is assigned to the correct target field. Also, make sure the Main Camera has a Cinemachine Brain and that the Cinemachine Camera is active.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 2: The camera movement feels jerky
&lt;/h3&gt;

&lt;p&gt;Check whether your player movement is smooth and whether multiple scripts are trying to control the camera. Avoid manually changing the Main Camera's transform while Cinemachine is controlling it.&lt;/p&gt;

&lt;p&gt;Also, check your update settings if your player moves through physics. Inconsistent movement timing can contribute to visible jitter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 3: The camera follows too slowly
&lt;/h3&gt;

&lt;p&gt;Reduce the damping values. If the camera is using a third-person follow component, check the available damping settings rather than changing unrelated camera properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 4: The player disappears behind objects
&lt;/h3&gt;

&lt;p&gt;Use an appropriate camera collision solution for your Cinemachine setup. A camera collision extension or suitable collision component can help prevent the camera from passing through walls, depending on your installed version and configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 5: The camera shows the wrong view
&lt;/h3&gt;

&lt;p&gt;Check the Cinemachine Camera's position and rotation controls, target assignment, and priority. If you have multiple Cinemachine Cameras, make sure the intended camera is active and has the appropriate priority.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for a Smooth Camera Follow
&lt;/h2&gt;

&lt;p&gt;Before finishing your camera setup, keep these tips in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with a simple camera setup and add features only when needed.&lt;/li&gt;
&lt;li&gt;Choose damping values based on the speed and style of your game.&lt;/li&gt;
&lt;li&gt;Test camera movement at different player speeds.&lt;/li&gt;
&lt;li&gt;Keep the player visible during jumps and direction changes.&lt;/li&gt;
&lt;li&gt;Use camera collision handling in environments with walls and obstacles.&lt;/li&gt;
&lt;li&gt;Test on the target platform, especially if you are developing for mobile devices.&lt;/li&gt;
&lt;li&gt;Avoid adding unnecessary camera effects that distract from gameplay.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember, the best camera is one that supports the gameplay without drawing too much attention to itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Creating a smooth camera follow system in Unity becomes much easier with Cinemachine. You can install the package, create a Cinemachine Camera, assign your player as the target, configure the camera position, and adjust damping to get the movement you want.&lt;/p&gt;

&lt;p&gt;You don't need a complicated camera script to achieve a good result. Start with the basic setup, test it in your scene, and adjust the settings until the camera feels natural.&lt;/p&gt;

&lt;p&gt;Once you understand these fundamentals, you can explore more advanced features such as camera collision, mouse look, camera blending, and different camera behaviours for different gameplay situations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Did this tutorial help you create a smoother camera in Unity?&lt;/strong&gt; Share your experience in the comments, and let us know which Cinemachine feature you would like to learn next. If you found this guide useful, don't forget to share it with other Unity developers!&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Is Cinemachine free to use in Unity?
&lt;/h3&gt;

&lt;p&gt;Yes. Cinemachine is available as a Unity package and can be installed through the Package Manager. Check the package information for your Unity version to confirm compatibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Do I need to write C# code to make a camera follow the player?
&lt;/h3&gt;

&lt;p&gt;No. You can create a basic camera-follow system using Cinemachine's Inspector settings. C# code may be useful when you need custom behaviour or want to control the camera dynamically.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. What is damping in Cinemachine?
&lt;/h3&gt;

&lt;p&gt;Damping controls how gradually the camera responds to changes in its target's position or rotation. Higher damping generally produces a slower response, while lower damping makes the camera react more quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Can I use Cinemachine for a 2D game?
&lt;/h3&gt;

&lt;p&gt;Yes. Cinemachine supports 2D camera setups. You can configure the camera to follow a 2D player and adjust framing and damping to create smooth movement across your level.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Why does my Cinemachine camera shake when the player moves?
&lt;/h3&gt;

&lt;p&gt;Camera shake or jitter can occur for several reasons, including inconsistent player movement, physics update timing, multiple scripts controlling the camera, or unsuitable damping settings. Check your movement and camera configuration first, then adjust the relevant settings to identify the cause.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you found this article useful:&lt;/p&gt;

&lt;p&gt;👍 Give it a like to show your support.&lt;br&gt;
🔄 Share it with other Unity developers who might find it helpful.&lt;br&gt;
💬 Leave a comment if you have faced a different coroutine problem or have another solution to share.&lt;br&gt;
🚀 Follow us for more practical &lt;strong&gt;&lt;a href="//learngamestutorial.com"&gt;Unity tutorials&lt;/a&gt;&lt;/strong&gt;, C# tips, and game development guides.&lt;br&gt;
Have a great time building and debugging your games!&lt;/p&gt;

&lt;p&gt;Happy Game Development! 🎮&lt;/p&gt;

</description>
      <category>programming</category>
      <category>unity3d</category>
      <category>camera</category>
      <category>cinemachine</category>
    </item>
    <item>
      <title>Unity Memory Leak: How to Find and Fix Memory Leaks in Unity Games</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Tue, 01 Dec 2026 22:24:00 +0000</pubDate>
      <link>https://dev.to/alok_krishali/unity-memory-leak-how-to-find-and-fix-memory-leaks-in-unity-games-4fa6</link>
      <guid>https://dev.to/alok_krishali/unity-memory-leak-how-to-find-and-fix-memory-leaks-in-unity-games-4fa6</guid>
      <description>&lt;p&gt;A &lt;strong&gt;Unity memory leak&lt;/strong&gt; can be difficult to notice when you are developing a game. Everything may work perfectly during the first few minutes, but after playing several levels, switching scenes, or spawning hundreds of objects, memory usage starts increasing. Eventually, your game may experience lag, stuttering, long loading times, or even crash. Knowing how to apply a proper &lt;strong&gt;Unity memory leak fix&lt;/strong&gt; and use the &lt;strong&gt;&lt;a href="https://unity.com/resources/improving-app-performance-with-new-memory-profiler" rel="noopener noreferrer"&gt;Unity Memory Profiler&lt;/a&gt;&lt;/strong&gt; can help you identify what is consuming &lt;strong&gt;&lt;a href="https://docs.unity.com/en-us/engine/6000.0/manual/analysis/performance-memory/overview?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;memory &lt;/a&gt;&lt;/strong&gt;and why it is not being released.&lt;/p&gt;

&lt;p&gt;Memory management is an important part of Unity game development, especially when building mobile games, 2D games, open-world games, or applications that run on devices with limited RAM.&lt;/p&gt;

&lt;p&gt;In this article, we will explore what causes memory leaks in Unity, how to detect them, and how to fix common memory-related problems using practical C# examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Memory Leak in Unity?
&lt;/h2&gt;

&lt;p&gt;A memory leak happens when your game keeps holding memory that it no longer needs.&lt;/p&gt;

&lt;p&gt;For example, imagine a game where the player completes a level and moves to the next one. The previous level's enemies, textures, audio clips, and other objects should no longer be needed.&lt;/p&gt;

&lt;p&gt;However, if something still holds references to those objects, they may remain in memory even after leaving the scene.&lt;/p&gt;

&lt;p&gt;Over time, this unnecessary memory usage can grow.&lt;/p&gt;

&lt;p&gt;A typical memory leak can cause:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increasing RAM usage during gameplay.&lt;/li&gt;
&lt;li&gt;Game crashes on low-memory devices.&lt;/li&gt;
&lt;li&gt;Longer loading times when switching scenes.&lt;/li&gt;
&lt;li&gt;Unnecessary garbage collection.&lt;/li&gt;
&lt;li&gt;Performance drops after extended gameplay.&lt;/li&gt;
&lt;li&gt;Unexpected memory usage after destroying GameObjects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One important thing to understand is that not every increase in memory usage is a memory leak. Unity sometimes keeps reserved memory for future allocations. You need to investigate whether memory usage continues growing unnecessarily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Memory Management in Unity
&lt;/h2&gt;

&lt;p&gt;Before fixing memory leaks, it helps to understand how Unity manages memory.&lt;/p&gt;

&lt;p&gt;Unity primarily works with three memory categories.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Managed Memory
&lt;/h3&gt;

&lt;p&gt;Managed memory is used by C# objects and is controlled by the garbage collector (GC).&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerData&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;playerName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you create an instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;PlayerData&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PlayerData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C# allocates memory for that object.&lt;/p&gt;

&lt;p&gt;When the object is no longer referenced, it becomes eligible for garbage collection.&lt;/p&gt;

&lt;p&gt;However, if another object still holds a reference to it, the garbage collector cannot collect it.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Native Memory
&lt;/h3&gt;

&lt;p&gt;Unity uses native C++ memory internally for many engine objects.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GameObjects and Components.&lt;/li&gt;
&lt;li&gt;Textures and Sprites.&lt;/li&gt;
&lt;li&gt;AudioClips.&lt;/li&gt;
&lt;li&gt;Meshes and Materials.&lt;/li&gt;
&lt;li&gt;RenderTextures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These objects have native memory associated with them, even when you access them through C# references.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Texture2D&lt;/span&gt; &lt;span class="n"&gt;texture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Texture2D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The texture consumes native memory in addition to the managed wrapper.&lt;/p&gt;

&lt;p&gt;Destroying the object when it is no longer required is important.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;texture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Unmanaged C# Memory
&lt;/h3&gt;

&lt;p&gt;Unity also supports unmanaged memory through APIs such as &lt;code&gt;NativeArray&lt;/code&gt; and other Unity Collections.&lt;/p&gt;

&lt;p&gt;This memory is not automatically managed by the regular garbage collector.&lt;/p&gt;

&lt;p&gt;You must release it manually.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;Unity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MemoryExample&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;NativeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NativeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Allocator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Persistent&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsCreated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you allocate native containers using persistent allocation and forget to dispose of them, you can introduce memory leaks.&lt;/p&gt;

&lt;h2&gt;
  
  
  7 Common Causes of Memory Leaks in Unity Games
&lt;/h2&gt;

&lt;p&gt;Let's look at some common problems that can cause unnecessary memory retention in Unity projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Static Variables Holding References
&lt;/h3&gt;

&lt;p&gt;Static variables remain accessible throughout the lifetime of their application domain.&lt;/p&gt;

&lt;p&gt;Consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;currentEnemy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;currentEnemy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FindWithTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Enemy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine that the enemy is destroyed when the player completes a level.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentEnemy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If other static collections or references continue holding related objects or managed data, those objects may remain reachable.&lt;/p&gt;

&lt;p&gt;A better approach is to clear references when they are no longer needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;currentEnemy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;ClearEnemy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;currentEnemy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Pay special attention to static lists, dictionaries, caches, and singleton managers that store references to scene objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Event Listeners Not Unsubscribed
&lt;/h3&gt;

&lt;p&gt;This is one of the most common sources of unintended object retention.&lt;/p&gt;

&lt;p&gt;Consider a health system that subscribes to a global event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HealthUI&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GameManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OnHealthChanged&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nc"&gt;UpdateHealth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GameManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OnHealthChanged&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;UpdateHealth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;UpdateHealth&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;health&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part is unsubscribing from the event.&lt;/p&gt;

&lt;p&gt;If a long-lived event publisher holds a reference to a listener, that listener may remain reachable even after you expect it to be removed.&lt;/p&gt;

&lt;p&gt;Always unsubscribe from events when the listener is no longer required.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Not Destroying Instantiated GameObjects
&lt;/h3&gt;

&lt;p&gt;Creating objects repeatedly without cleaning them up can increase memory consumption.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnemySpawner&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;enemyPrefab&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;SpawnEnemy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemyPrefab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your game continuously spawns enemies, the number of active GameObjects can grow quickly.&lt;/p&gt;

&lt;p&gt;You should destroy enemies when they are no longer required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemyObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For games that frequently spawn and remove objects, consider using &lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-object-pooling/" rel="noopener noreferrer"&gt;Unity Object Pooling&lt;/a&gt;&lt;/strong&gt; instead of repeatedly creating and destroying objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-object-pooling/" rel="noopener noreferrer"&gt;Object pooling&lt;/a&gt;&lt;/strong&gt; is particularly useful for bullets, particles, enemies, and other frequently reused objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Not Releasing Textures and Assets
&lt;/h3&gt;

&lt;p&gt;Textures can consume a significant amount of memory, especially in mobile games.&lt;/p&gt;

&lt;p&gt;Consider loading a texture dynamically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Texture2D&lt;/span&gt; &lt;span class="n"&gt;texture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nc"&gt;Resources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Load&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Texture2D&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Images/Background"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the texture is no longer needed, remove your references and release unused assets when appropriate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;texture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Resources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UnloadUnusedAssets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Resources.UnloadUnusedAssets()&lt;/code&gt; asks Unity to unload assets that are no longer referenced and can be unloaded.&lt;/p&gt;

&lt;p&gt;However, it is not an immediate replacement for proper asset lifecycle management. Use it at suitable points, such as after a major scene transition, rather than calling it repeatedly during gameplay.&lt;/p&gt;

&lt;p&gt;If you are using Addressables, release loaded assets using the corresponding Addressables release methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Creating Too Many Temporary Objects
&lt;/h3&gt;

&lt;p&gt;Creating temporary objects inside frequently executed methods can generate unnecessary garbage collection allocations.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Score: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates new strings repeatedly.&lt;/p&gt;

&lt;p&gt;Another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;enemies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating a new list every frame is unnecessary if you can reuse an existing collection.&lt;/p&gt;

&lt;p&gt;Instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;enemies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;enemies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Reuse the existing collection.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reducing unnecessary allocations can help reduce GC pressure and improve performance.&lt;/p&gt;

&lt;p&gt;Remember that garbage collection allocations are not automatically memory leaks, but excessive allocations can contribute to performance problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Not Releasing RenderTextures
&lt;/h3&gt;

&lt;p&gt;RenderTextures are commonly used for cameras, reflections, minimaps, and other rendering effects.&lt;/p&gt;

&lt;p&gt;If you create RenderTextures dynamically, you should release them when they are no longer needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CameraTexture&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;RenderTexture&lt;/span&gt; &lt;span class="n"&gt;renderTexture&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;renderTexture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RenderTexture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;renderTexture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;renderTexture&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;renderTexture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nc"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;renderTexture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;renderTexture&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember that &lt;code&gt;Release()&lt;/code&gt; frees the hardware resources associated with the RenderTexture, while &lt;code&gt;Destroy()&lt;/code&gt; removes the Unity object.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Forgetting to Dispose Native Containers
&lt;/h3&gt;

&lt;p&gt;If you use Unity Jobs, Burst, or Collections, you may work with NativeArray, NativeList, and other native containers.&lt;/p&gt;

&lt;p&gt;These allocations require proper cleanup.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;Unity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NativeMemoryExample&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;NativeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NativeArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Allocator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Persistent&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IsCreated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Forgetting to dispose of persistent native allocations can cause memory leaks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Always check whether a native container has been created before disposing of it. Also ensure that you do not dispose of it while a Job is still using it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Find Memory Leaks Using Unity Memory Profiler
&lt;/h2&gt;

&lt;p&gt;Finding the actual source of a memory leak is often more challenging than fixing it.&lt;/p&gt;

&lt;p&gt;Fortunately, Unity provides profiling tools that help developers understand memory usage.&lt;/p&gt;

&lt;p&gt;The two important tools are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unity Profiler (Memory Module).&lt;/li&gt;
&lt;li&gt;Unity Memory Profiler package.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 1: Install Unity Memory Profiler
&lt;/h3&gt;

&lt;p&gt;Open your Unity project.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to Window.&lt;/li&gt;
&lt;li&gt;Open Package Manager.&lt;/li&gt;
&lt;li&gt;Search for Memory Profiler.&lt;/li&gt;
&lt;li&gt;Install the package.&lt;/li&gt;
&lt;li&gt;Open Window → Analysis → Memory Profiler.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The Memory Profiler provides a detailed view of memory usage and allows you to capture memory snapshots.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Capture the First Memory Snapshot
&lt;/h3&gt;

&lt;p&gt;Start your game and reach a point where you want to measure memory usage.&lt;/p&gt;

&lt;p&gt;For example, capture a snapshot after the main menu has loaded.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Screenshot suggestion: Show Unity Memory Profiler with the first memory snapshot captured.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This snapshot will act as your baseline.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Play the Game and Capture Another Snapshot
&lt;/h3&gt;

&lt;p&gt;Now perform some gameplay actions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load a level.&lt;/li&gt;
&lt;li&gt;Spawn enemies.&lt;/li&gt;
&lt;li&gt;Collect rewards.&lt;/li&gt;
&lt;li&gt;Load another scene.&lt;/li&gt;
&lt;li&gt;Return to the previous scene.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Repeat these actions several times.&lt;/p&gt;

&lt;p&gt;Capture another memory snapshot after completing the same sequence.&lt;/p&gt;

&lt;p&gt;The purpose is to see whether objects that should have been removed are still present.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Compare Memory Snapshots
&lt;/h3&gt;

&lt;p&gt;This is one of the most useful features of Unity Memory Profiler.&lt;/p&gt;

&lt;p&gt;Open the Compare Snapshots view and select your two captured snapshots.&lt;/p&gt;

&lt;p&gt;Look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increasing GameObject counts.&lt;/li&gt;
&lt;li&gt;Unreleased Texture2D objects.&lt;/li&gt;
&lt;li&gt;Growing AudioClip counts.&lt;/li&gt;
&lt;li&gt;Unexpected Mesh or Material objects.&lt;/li&gt;
&lt;li&gt;Managed objects that remain referenced.&lt;/li&gt;
&lt;li&gt;Objects that should have disappeared after unloading a scene.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Screenshot suggestion: Show Unity Memory Profiler Compare Snapshots view with memory differences highlighted.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you repeatedly load and unload the same scene, but the number of scene-related objects keeps increasing, investigate their references and asset lifecycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Inspect Object References
&lt;/h3&gt;

&lt;p&gt;Finding an object that remains in memory is only the beginning.&lt;/p&gt;

&lt;p&gt;You also need to understand why it is still there.&lt;/p&gt;

&lt;p&gt;Select the object in Memory Profiler and inspect its references where available.&lt;/p&gt;

&lt;p&gt;Look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static references.&lt;/li&gt;
&lt;li&gt;Event subscriptions.&lt;/li&gt;
&lt;li&gt;Singleton managers.&lt;/li&gt;
&lt;li&gt;Cached objects.&lt;/li&gt;
&lt;li&gt;Collections that are never cleared.&lt;/li&gt;
&lt;li&gt;Assets that are still referenced by other objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This helps you identify the actual source of the memory retention rather than randomly changing code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Profile a development build on your target device whenever possible. Unity Editor profiling includes additional memory overhead and may not accurately represent the memory usage of your final game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity Profiler: How to Monitor Memory Usage
&lt;/h2&gt;

&lt;p&gt;You don't always need the detailed Memory Profiler to identify a problem.&lt;/p&gt;

&lt;p&gt;Unity's built-in Profiler also provides useful memory information.&lt;/p&gt;

&lt;p&gt;Open:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Window → Analysis → Profiler&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select the Memory module.&lt;/p&gt;

&lt;p&gt;You can monitor:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Total Used Memory.&lt;/li&gt;
&lt;li&gt;Total Reserved Memory.&lt;/li&gt;
&lt;li&gt;GC Used Memory.&lt;/li&gt;
&lt;li&gt;GC Allocated In Frame.&lt;/li&gt;
&lt;li&gt;Texture Memory.&lt;/li&gt;
&lt;li&gt;Mesh Memory.&lt;/li&gt;
&lt;li&gt;Object counts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pay special attention to GC Allocated In Frame.&lt;/p&gt;

&lt;p&gt;If your game creates unnecessary managed allocations every frame, you may notice repeated allocation spikes.&lt;/p&gt;

&lt;p&gt;You can also use the CPU Usage module to inspect GC.Alloc samples and their call stacks to locate the code responsible for managed allocations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Example: Fixing a Scene Memory Leak
&lt;/h2&gt;

&lt;p&gt;Let's consider a simple example.&lt;/p&gt;

&lt;p&gt;Imagine your game has three levels. Every time the player completes a level, the next scene loads.&lt;/p&gt;

&lt;p&gt;After playing several levels, memory usage continues increasing.&lt;/p&gt;

&lt;p&gt;A possible problem is that a global manager keeps references to objects from previously loaded scenes.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;enemies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;RegisterEnemy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;enemies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the scene changes, the old enemies are destroyed, but the static list still holds references to them.&lt;/p&gt;

&lt;p&gt;This can prevent managed objects and their associated references from being collected as expected.&lt;/p&gt;

&lt;p&gt;A better approach is to clear the collection when the scene's enemies are no longer needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;enemies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;RegisterEnemy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;enemies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;ClearEnemies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;enemies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call &lt;code&gt;ClearEnemies()&lt;/code&gt; at the appropriate point in your scene transition or level cleanup process.&lt;/p&gt;

&lt;p&gt;Also make sure any independently owned resources are released properly.&lt;/p&gt;

&lt;p&gt;The important lesson is that destroying a GameObject does not automatically remove every reference to its related data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices to Prevent Memory Leaks in Unity
&lt;/h2&gt;

&lt;p&gt;Here are some practical habits that can help you avoid memory problems in your Unity projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Use Object Pooling
&lt;/h3&gt;

&lt;p&gt;Avoid repeatedly instantiating and destroying frequently used objects.&lt;/p&gt;

&lt;p&gt;Use object pooling for bullets, enemies, particles, and UI elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Clean Up Event Subscriptions
&lt;/h3&gt;

&lt;p&gt;Always unsubscribe from events when the listener is no longer active or required.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Release Dynamically Loaded Assets
&lt;/h3&gt;

&lt;p&gt;Use proper asset lifecycle management with Resources or Addressables.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Dispose Native Containers
&lt;/h3&gt;

&lt;p&gt;Always dispose of NativeArray, NativeList, and other disposable native allocations when they are no longer required.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Avoid Unnecessary Allocations
&lt;/h3&gt;

&lt;p&gt;Reduce unnecessary object creation, string operations, LINQ usage, and temporary collections in frequently executed methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Monitor Static References
&lt;/h3&gt;

&lt;p&gt;Be careful when using static variables, singleton managers, and global collections.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Profile on Real Devices
&lt;/h3&gt;

&lt;p&gt;Do not rely entirely on Editor memory statistics. Test your development builds on the actual target hardware.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Test Scene Transitions
&lt;/h3&gt;

&lt;p&gt;Repeatedly load and unload scenes during testing. Check whether memory usage and object counts return to a stable level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity Memory Leak Debugging: Do's and Don'ts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frt1flg9qhagpaq1dlnxv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frt1flg9qhagpaq1dlnxv.png" alt="Do's and Don'ts of Unity Memory Leak Debugging" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One additional point: calling &lt;code&gt;GC.Collect()&lt;/code&gt; is not a proper solution for memory leaks. It can trigger garbage collection, but it cannot collect objects that are still referenced or release resources that require explicit cleanup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Memory leaks can slowly affect the stability and performance of Unity games. They are especially important to investigate in mobile games, where available memory is often limited.&lt;/p&gt;

&lt;p&gt;The good news is that most memory-related problems become easier to understand when you use the right profiling tools and follow a consistent debugging process.&lt;/p&gt;

&lt;p&gt;Here are the key takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand the difference between managed, native, and unmanaged memory.&lt;/li&gt;
&lt;li&gt;Use Unity Memory Profiler to capture and compare snapshots.&lt;/li&gt;
&lt;li&gt;Check static references and event subscriptions.&lt;/li&gt;
&lt;li&gt;Destroy unused GameObjects and release dynamically loaded assets.&lt;/li&gt;
&lt;li&gt;Dispose of NativeArray and other native containers.&lt;/li&gt;
&lt;li&gt;Reduce unnecessary allocations and use object pooling.&lt;/li&gt;
&lt;li&gt;Test scene transitions and profile on actual devices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;My recommendation:&lt;/strong&gt; Make memory profiling part of your regular game testing process instead of waiting until players report crashes. Finding unnecessary memory retention early can save a lot of debugging time later.&lt;/p&gt;

&lt;p&gt;If you found this article useful, share it with other Unity developers. Feel free to comment below with your experience or memory-related problems you've faced in your Unity projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. How do I check for memory leaks in Unity?
&lt;/h3&gt;

&lt;p&gt;Use Unity Memory Profiler to capture memory snapshots at different stages of gameplay. Compare the snapshots and investigate objects that remain in memory unexpectedly.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. What is the best tool for finding memory leaks in Unity?
&lt;/h3&gt;

&lt;p&gt;Unity Memory Profiler is one of the &lt;a href="https://unity.com/how-to/analyze-memory-usage-memory-profiling-tools?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;most useful tools for detailed memory analysis&lt;/a&gt;. It allows you to capture snapshots, compare memory usage, and inspect Unity objects and their references.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Does Destroy() release memory immediately in Unity?
&lt;/h3&gt;

&lt;p&gt;No. Destroy() schedules a Unity object for destruction, generally at the end of the current Update loop. Associated managed memory is reclaimed by garbage collection when it becomes eligible, while native resources follow Unity's object lifecycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Does garbage collection fix memory leaks in Unity?
&lt;/h3&gt;

&lt;p&gt;No. Garbage collection only reclaims managed objects that are no longer reachable. If an object is still referenced by a static variable, event, or collection, the garbage collector cannot remove it.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. How do I reduce memory usage in Unity mobile games?
&lt;/h3&gt;

&lt;p&gt;Optimize textures and audio, use object pooling, release unused assets, avoid unnecessary allocations, dispose of native containers, and regularly profile your game on real Android and iOS devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also Check:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-webgl-optimization/" rel="noopener noreferrer"&gt;Unity webGL Optimization&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-game-optimization-tips/" rel="noopener noreferrer"&gt;Unity Game Optimization Tips&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you found this article useful:&lt;/p&gt;

&lt;p&gt;👍 Give it a like to show your support.&lt;br&gt;
🔄 Share it with other Unity developers who might find it helpful.&lt;br&gt;
💬 Leave a comment if you have faced a different coroutine problem or have another solution to share.&lt;br&gt;
🚀 Follow us for more &lt;strong&gt;&lt;a href="https://learngamestutorial.com/" rel="noopener noreferrer"&gt;practical Unity tutorials&lt;/a&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;a href="https://learngamestutorial.com/" rel="noopener noreferrer"&gt;C# tips&lt;/a&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;a href="https://learngamestutorial.com/" rel="noopener noreferrer"&gt;game development guides&lt;/a&gt;&lt;/strong&gt;.&lt;br&gt;
Have a great time building and debugging your games!&lt;/p&gt;

&lt;p&gt;Happy Game Development! 🎮&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>unity3d</category>
      <category>performance</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>Unity Coroutine Not Working? 10 Common Problems and Their Solutions</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Wed, 25 Nov 2026 11:00:46 +0000</pubDate>
      <link>https://dev.to/alok_krishali/unity-coroutine-not-working-10-common-problems-and-their-solutions-529c</link>
      <guid>https://dev.to/alok_krishali/unity-coroutine-not-working-10-common-problems-and-their-solutions-529c</guid>
      <description>&lt;p&gt;&lt;strong&gt;Unity Coroutine&lt;/strong&gt; not working as expected? You are not alone! Sometimes a coroutine doesn't start, stops unexpectedly, never reaches the next line, or simply doesn't execute after &lt;code&gt;WaitForSeconds()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I have faced these kinds of situations while working on Unity gameplay systems. In many cases, the problem is not with the &lt;a href="https://docs.unity3d.com/6000.2/Documentation/ScriptReference/Coroutine.html" rel="noopener noreferrer"&gt;coroutine &lt;/a&gt;itself but with how it is started, stopped, or managed.&lt;/p&gt;

&lt;p&gt;For example, you might call &lt;code&gt;StartCoroutine()&lt;/code&gt; but never see the expected output. Or your coroutine might wait forever because &lt;code&gt;Time.timeScale&lt;/code&gt; is set to zero.&lt;/p&gt;

&lt;p&gt;In this article, we will explore &lt;strong&gt;10 common Unity Coroutine problems and their solutions&lt;/strong&gt; with practical C# examples. We will also understand why these problems happen and how to avoid them in your projects.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Coroutine Is Not Starting
&lt;/h2&gt;

&lt;p&gt;This is one of the most common problems when &lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-coroutine-ienumerator-waitforseconds/" rel="noopener noreferrer"&gt;working with Unity Coroutines&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You create an &lt;code&gt;IEnumerator&lt;/code&gt; method, write your logic inside it, and expect Unity to execute it. But nothing happens.&lt;/p&gt;

&lt;p&gt;The reason might be that you forgot to call &lt;code&gt;StartCoroutine()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incorrect approach:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoroutineExample&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;MyCoroutine()&lt;/code&gt; creates an IEnumerator but does not start it through Unity's coroutine scheduler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;StartCoroutine()&lt;/code&gt; to execute it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Unity will execute the coroutine and continue it after the specified delay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; Calling an IEnumerator method and starting a coroutine are two different things.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Coroutine Stops When GameObject Is Disabled or Destroyed
&lt;/h2&gt;

&lt;p&gt;Another common issue occurs when the GameObject running the coroutine gets disabled or destroyed.&lt;/p&gt;

&lt;p&gt;For example, imagine you start a coroutine on an enemy GameObject. After 2 seconds, you disable that enemy. You might expect the coroutine to continue, but it will stop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the GameObject containing this script is disabled or destroyed during the wait, the coroutine will stop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you need an operation to continue after disabling an enemy or another GameObject, consider running the coroutine from a separate manager GameObject that remains active.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoroutineManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nc"&gt;CoroutineManager&lt;/span&gt; &lt;span class="nc"&gt;Instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;StartMyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now another script can start the coroutine using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;CoroutineManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Instance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StartMyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Disabling a GameObject stops its coroutines. However, disabling only the MonoBehaviour component does not automatically stop its coroutines. Destroying the GameObject or unloading its scene also stops them.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. WaitForSeconds Is Not Working
&lt;/h2&gt;

&lt;p&gt;Sometimes a coroutine starts successfully, but the code after &lt;code&gt;WaitForSeconds()&lt;/code&gt; never executes.&lt;/p&gt;

&lt;p&gt;One possible reason is &lt;code&gt;Time.timeScale&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timeScale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pauses scaled game time.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;WaitForSeconds()&lt;/code&gt; depends on scaled time, its countdown will not progress while &lt;code&gt;Time.timeScale&lt;/code&gt; is zero.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the game is paused using &lt;code&gt;Time.timeScale = 0f&lt;/code&gt;, the second message will not appear until scaled time starts progressing again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;WaitForSecondsRealtime()&lt;/code&gt; if you want to wait using real time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSecondsRealtime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When should you use it?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;WaitForSeconds()&lt;/code&gt; for gameplay delays.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;WaitForSecondsRealtime()&lt;/code&gt; for pause menus, UI messages, and real-time operations that should continue while the game is paused.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Coroutine Runs Multiple Times Unexpectedly
&lt;/h2&gt;

&lt;p&gt;Have you ever noticed that the same coroutine starts multiple times?&lt;/p&gt;

&lt;p&gt;This usually happens when you call &lt;code&gt;StartCoroutine()&lt;/code&gt; repeatedly without checking whether an existing coroutine is already running.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetKeyDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Space&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the player repeatedly presses Space, multiple instances of the coroutine can run simultaneously.&lt;/p&gt;

&lt;p&gt;This may cause unexpected behaviour, especially when handling animations, attacks, or enemy spawning.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Store a Coroutine reference and check whether it is already running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Coroutine&lt;/span&gt; &lt;span class="n"&gt;myRoutine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetKeyDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Space&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myRoutine&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;myRoutine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;myRoutine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the coroutine will not start again while the previous instance is running.&lt;/p&gt;

&lt;p&gt;This is particularly useful for weapon reloads, attack animations, cooldowns, and timed gameplay events.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. StopCoroutine Is Not Stopping the Coroutine
&lt;/h2&gt;

&lt;p&gt;Sometimes you call &lt;code&gt;StopCoroutine()&lt;/code&gt;, but the coroutine continues running.&lt;/p&gt;

&lt;p&gt;This can happen when you use a different reference or overload to stop it than the one used to start it.&lt;/p&gt;

&lt;p&gt;For example, using a newly created IEnumerator instance to stop a coroutine that was started using another instance may not stop the intended routine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problematic approach:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;StopMyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;StopCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second call creates another IEnumerator instance. It is not the same running coroutine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Store the Coroutine reference when starting it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Coroutine&lt;/span&gt; &lt;span class="n"&gt;myRoutine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;myRoutine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;StopMyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myRoutine&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;StopCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myRoutine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;myRoutine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Coroutine Running"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can stop the exact coroutine instance using its reference.&lt;/p&gt;

&lt;p&gt;You can also use &lt;code&gt;StopAllCoroutines()&lt;/code&gt; to stop all coroutines running on the same MonoBehaviour, but use it carefully.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Coroutine Gets Stuck in an Infinite Loop
&lt;/h2&gt;

&lt;p&gt;Another issue is creating an infinite loop inside a coroutine without yielding control back to Unity.&lt;/p&gt;

&lt;p&gt;Consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Running"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is dangerous!&lt;/p&gt;

&lt;p&gt;The loop never gives control back to Unity. It can freeze the main thread and make the game unresponsive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add a yield statement inside the loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Running"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;yield return null&lt;/code&gt; pauses the coroutine until the next frame.&lt;/p&gt;

&lt;p&gt;You can also use a time delay:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; Always make sure that long-running coroutine loops yield control regularly.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Coroutine Is Not Reaching the Next Statement
&lt;/h2&gt;

&lt;p&gt;Sometimes your coroutine starts, prints the first message, and never reaches the next statement.&lt;/p&gt;

&lt;p&gt;This often happens when you use &lt;code&gt;WaitUntil()&lt;/code&gt; or &lt;code&gt;WaitWhile()&lt;/code&gt; with a condition that never changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isPlayerReady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Waiting for player..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;isPlayerReady&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Player Ready!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The coroutine waits until &lt;code&gt;isPlayerReady&lt;/code&gt; becomes true.&lt;/p&gt;

&lt;p&gt;But if you never update this variable, the coroutine will keep waiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Make sure the condition is updated correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetKeyDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Space&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;isPlayerReady&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now pressing Space changes the condition, allowing the coroutine to continue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Debugging tip:&lt;/strong&gt; Add Debug.Log statements before and after &lt;code&gt;WaitUntil()&lt;/code&gt; or &lt;code&gt;WaitWhile()&lt;/code&gt; to identify where execution gets stuck.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Nested Coroutine Is Not Waiting for the Other Coroutine
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to execute one coroutine after another.&lt;/p&gt;

&lt;p&gt;For example, you want to complete an animation, wait for 2 seconds, and then spawn an object.&lt;/p&gt;

&lt;p&gt;A common mistake is starting another coroutine without yielding it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incorrect approach:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MainCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FirstCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Main Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;FirstCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"First Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;FirstCoroutine()&lt;/code&gt; starts separately. The main coroutine does not wait for it to finish.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;yield return StartCoroutine()&lt;/code&gt; when you want sequential execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MainCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FirstCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Main Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;FirstCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"First Coroutine Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the main coroutine waits until the first coroutine completes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember the difference:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;StartCoroutine()&lt;/code&gt; starts another coroutine.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;yield return StartCoroutine()&lt;/code&gt; waits for the nested coroutine to finish before continuing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. Coroutine Is Not Running After Scene Change
&lt;/h2&gt;

&lt;p&gt;Have you ever started a coroutine and then changed the scene, only to discover that it stopped?&lt;/p&gt;

&lt;p&gt;This is expected behaviour when the GameObject running the coroutine gets destroyed during scene unloading.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;LoadNextLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SceneManagement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SceneManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LoadScene&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Level2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the coroutine's GameObject gets destroyed before the delay finishes, the scene loading operation will never happen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use a persistent manager GameObject with &lt;code&gt;DontDestroyOnLoad()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;DontDestroyOnLoad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;StartLevelLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoadNextLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;LoadNextLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SceneManagement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SceneManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LoadScene&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Level2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows the manager to survive scene changes, so its coroutine can continue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Make sure you don't accidentally create multiple persistent GameManagers when loading new scenes.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Coroutine Throws an Exception and Stops Unexpectedly
&lt;/h2&gt;

&lt;p&gt;Sometimes a coroutine starts correctly but stops before reaching the expected result.&lt;/p&gt;

&lt;p&gt;One possible reason is an exception inside the coroutine.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;enemy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;enemy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;enemy&lt;/code&gt; is null, so accessing &lt;code&gt;SetActive()&lt;/code&gt; throws a NullReferenceException.&lt;/p&gt;

&lt;p&gt;The remaining coroutine statements will not execute normally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check references before using them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;IEnumerator&lt;/span&gt; &lt;span class="nc"&gt;MyCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Started"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enemy&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;enemy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LogWarning&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Enemy reference is missing!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, check the Unity Console for errors and stack traces whenever a coroutine behaves unexpectedly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My recommendation:&lt;/strong&gt; Don't ignore Console errors. A small null reference or missing component can be the reason your entire gameplay sequence fails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Quick Unity Coroutine Debugging Checklist
&lt;/h2&gt;

&lt;p&gt;Before spending too much time debugging, check these common points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the coroutine started using &lt;code&gt;StartCoroutine()&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Is the GameObject active?&lt;/li&gt;
&lt;li&gt;Has the coroutine been stopped somewhere else?&lt;/li&gt;
&lt;li&gt;Is &lt;code&gt;Time.timeScale&lt;/code&gt; set to zero?&lt;/li&gt;
&lt;li&gt;Is a &lt;code&gt;WaitUntil()&lt;/code&gt; condition actually becoming true?&lt;/li&gt;
&lt;li&gt;Are you accidentally starting multiple coroutine instances?&lt;/li&gt;
&lt;li&gt;Does every infinite loop contain a yield statement?&lt;/li&gt;
&lt;li&gt;Are you using the correct Coroutine reference?&lt;/li&gt;
&lt;li&gt;Is the coroutine's GameObject destroyed during a scene change?&lt;/li&gt;
&lt;li&gt;Are there any exceptions in the Unity Console?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These simple checks can help you identify many coroutine problems quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Unity Coroutine&lt;/strong&gt;s are extremely useful for handling delays, animations, enemy spawning, countdowns, and other time-based gameplay operations.&lt;/p&gt;

&lt;p&gt;However, understanding how they work is just as important as knowing how to write them.&lt;/p&gt;

&lt;p&gt;Most &lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-coroutine-ienumerator-waitforseconds/" rel="noopener noreferrer"&gt;Unity Coroutine&lt;/a&gt;&lt;/strong&gt; problems are caused by incorrect starting or stopping, GameObject lifecycle issues, time scale settings, missing yield statements, or conditions that never become true.&lt;/p&gt;

&lt;p&gt;Let's quickly revise everything we learned.&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick Summary: 10 Common Coroutine Problems
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Coroutine not starting:&lt;/strong&gt; Always use &lt;code&gt;StartCoroutine()&lt;/code&gt; to start an IEnumerator routine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GameObject disabled:&lt;/strong&gt; Coroutines stop when their GameObject is disabled or destroyed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WaitForSeconds not working:&lt;/strong&gt; Check &lt;code&gt;Time.timeScale&lt;/code&gt;; use &lt;code&gt;WaitForSecondsRealtime()&lt;/code&gt; when needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple executions:&lt;/strong&gt; Store Coroutine references and prevent unnecessary duplicate starts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StopCoroutine not working:&lt;/strong&gt; Stop the correct running coroutine instance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infinite loop:&lt;/strong&gt; Always yield control inside long-running loops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Waiting forever:&lt;/strong&gt; Check your &lt;code&gt;WaitUntil()&lt;/code&gt; and &lt;code&gt;WaitWhile()&lt;/code&gt; conditions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nested coroutine issues:&lt;/strong&gt; Use &lt;code&gt;yield return StartCoroutine()&lt;/code&gt; for sequential execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scene change issues:&lt;/strong&gt; Use a persistent manager when an operation must survive scene unloading.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unexpected exceptions:&lt;/strong&gt; Check references and inspect Unity Console errors.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Do's and Don'ts When Using Unity Coroutines
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Do:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;StartCoroutine()&lt;/code&gt; to execute coroutine methods.&lt;/li&gt;
&lt;li&gt;Store Coroutine references when you need to manage them.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;WaitForSecondsRealtime()&lt;/code&gt; for real-time delays.&lt;/li&gt;
&lt;li&gt;Add yield statements inside long-running loops.&lt;/li&gt;
&lt;li&gt;Check conditions before using &lt;code&gt;WaitUntil()&lt;/code&gt; and &lt;code&gt;WaitWhile()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use Unity Console logs to debug unexpected behaviour.&lt;/li&gt;
&lt;li&gt;Keep coroutine management organized.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Don't:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't call an IEnumerator method and expect it to run automatically.&lt;/li&gt;
&lt;li&gt;Don't start the same coroutine repeatedly without checking its state.&lt;/li&gt;
&lt;li&gt;Don't use infinite loops without yielding.&lt;/li&gt;
&lt;li&gt;Don't forget that &lt;code&gt;Time.timeScale&lt;/code&gt; affects &lt;code&gt;WaitForSeconds()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Don't assume disabling a MonoBehaviour stops its coroutines.&lt;/li&gt;
&lt;li&gt;Don't ignore NullReferenceException errors.&lt;/li&gt;
&lt;li&gt;Don't use &lt;code&gt;StopAllCoroutines()&lt;/code&gt; when you only need to stop one specific routine.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understand Unity Coroutine with C# Intermediate Tutorial
&lt;/h2&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/5L9ksCs6MbE" width="710" height="399"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Why is my Unity Coroutine not starting?
&lt;/h3&gt;

&lt;p&gt;The most common reason is forgetting to call &lt;code&gt;StartCoroutine()&lt;/code&gt;. Also check whether the GameObject is active, the script is enabled, and there are any errors in the Unity Console.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Does disabling a GameObject stop a Coroutine in Unity?
&lt;/h3&gt;

&lt;p&gt;Yes. Disabling the GameObject stops its &lt;a href="https://docs.unity.com/en-us/engine/6000.6/manual/scripting/coroutines-section/coroutines" rel="noopener noreferrer"&gt;running coroutines&lt;/a&gt;. Destroying the GameObject also stops them. However, disabling only the MonoBehaviour component does not automatically stop its coroutines.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Why is WaitForSeconds not working in Unity?
&lt;/h3&gt;

&lt;p&gt;Check whether &lt;code&gt;Time.timeScale&lt;/code&gt; is set to zero. &lt;code&gt;WaitForSeconds()&lt;/code&gt; uses scaled game time, so it will not progress while the game is paused. Use &lt;code&gt;WaitForSecondsRealtime()&lt;/code&gt; if you need an unscaled delay.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. How can I prevent a Unity Coroutine from running multiple times?
&lt;/h3&gt;

&lt;p&gt;Store its reference in a &lt;code&gt;Coroutine&lt;/code&gt; variable and check whether it is already running before calling &lt;code&gt;StartCoroutine()&lt;/code&gt; again. Set the reference to null when the coroutine finishes.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Can I run multiple Coroutines at the same time in Unity?
&lt;/h3&gt;

&lt;p&gt;Yes! &lt;a href="https://unity.com/" rel="noopener noreferrer"&gt;Unity &lt;/a&gt;allows you to run multiple coroutines simultaneously. Each coroutine can manage its own execution and wait instructions. However, make sure you manage their references and stop unnecessary routines.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Thanks for reading!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope these 10 common &lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-coroutine-ienumerator-waitforseconds/" rel="noopener noreferrer"&gt;Unity Coroutine&lt;/a&gt;&lt;/strong&gt; problems and their solutions help you debug your projects more easily.&lt;/p&gt;

&lt;p&gt;If you found this article useful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Give it a like to show your support.&lt;/li&gt;
&lt;li&gt;🔄 Share it with other Unity developers who might find it helpful.&lt;/li&gt;
&lt;li&gt;💬 Leave a comment if you have faced a different coroutine problem or have another solution to share.&lt;/li&gt;
&lt;li&gt;🚀 Follow us for more practical Unity tutorials, C# tips, and game development guides.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have a great time building and debugging your games!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Game Development! 🎮&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>csharp</category>
      <category>programming</category>
      <category>coroutine</category>
    </item>
    <item>
      <title>Unity Update vs FixedUpdate: What I Use, What I Avoid, and Why</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Mon, 23 Nov 2026 11:53:11 +0000</pubDate>
      <link>https://dev.to/alok_krishali/unity-update-vs-fixedupdate-what-i-use-what-i-avoid-and-why-2gkk</link>
      <guid>https://dev.to/alok_krishali/unity-update-vs-fixedupdate-what-i-use-what-i-avoid-and-why-2gkk</guid>
      <description>&lt;p&gt;&lt;strong&gt;Unity Update vs FixedUpdate&lt;/strong&gt; is one of the most common topics that every Unity developer should understand. Choosing the wrong method can lead to inconsistent player movement, physics issues, and unexpected gameplay behaviour.&lt;/p&gt;

&lt;p&gt;When developing games in Unity, I use &lt;code&gt;Update()&lt;/code&gt; and &lt;code&gt;FixedUpdate()&lt;/code&gt; for different purposes. Although both methods run repeatedly during gameplay, they don't work in the same way.&lt;/p&gt;

&lt;p&gt;For example, should you read player input inside Update or FixedUpdate? Where should you move a Rigidbody? And when should you use LateUpdate?&lt;/p&gt;

&lt;p&gt;In this article, I'll explain the differences between these methods using simple examples. I'll also share some common mistakes I avoid in my Unity projects and explain when I use each method.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What Is Update() in Unity?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Update()&lt;/code&gt; is a Unity event function that runs once per rendered frame.&lt;/p&gt;

&lt;p&gt;Its execution depends on the game's frame rate (FPS).&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At 30 FPS, Update runs approximately 30 times per second.&lt;/li&gt;
&lt;li&gt;At 60 FPS, Update runs approximately 60 times per second.&lt;/li&gt;
&lt;li&gt;At 120 FPS, Update runs approximately 120 times per second.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means Update does not have a fixed execution time.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Should You Use Update()?
&lt;/h3&gt;

&lt;p&gt;I generally use Update for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading keyboard, mouse, and player input.&lt;/li&gt;
&lt;li&gt;Updating UI elements.&lt;/li&gt;
&lt;li&gt;Handling gameplay logic.&lt;/li&gt;
&lt;li&gt;Checking player actions.&lt;/li&gt;
&lt;li&gt;Updating timers and cooldowns.&lt;/li&gt;
&lt;li&gt;Moving objects without Rigidbody physics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: Player Movement Using Update()
&lt;/h3&gt;

&lt;p&gt;If your player doesn't use Rigidbody physics, you can move it directly using Update.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerMovement&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetAxis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Horizontal"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetAxis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Vertical"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;Space&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;World&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why do I use Time.deltaTime?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because Update runs at different frame rates. Without &lt;code&gt;Time.deltaTime&lt;/code&gt;, the player may move faster on devices with higher FPS and slower on devices with lower FPS.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Time.deltaTime&lt;/code&gt; helps keep movement speed consistent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My approach:&lt;/strong&gt; I use Update for regular gameplay logic, input reading, and non-physics-based movement.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What Is FixedUpdate() in Unity?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;FixedUpdate()&lt;/code&gt; is another Unity event function, but unlike Update, it runs at a fixed time interval.&lt;/p&gt;

&lt;p&gt;By default, Unity calls FixedUpdate every 0.02 seconds, which means 50 times per second.&lt;/p&gt;

&lt;p&gt;You can change this value from:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit → Project Settings → Time → Fixed Timestep&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FixedUpdate is mainly designed for physics-related operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Should You Use FixedUpdate()?
&lt;/h3&gt;

&lt;p&gt;I use FixedUpdate for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Moving Rigidbody objects.&lt;/li&gt;
&lt;li&gt;Applying forces.&lt;/li&gt;
&lt;li&gt;Applying torque.&lt;/li&gt;
&lt;li&gt;Physics-based jumping.&lt;/li&gt;
&lt;li&gt;Changing Rigidbody velocity.&lt;/li&gt;
&lt;li&gt;Performing physics-related calculations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: Rigidbody Movement Using FixedUpdate()
&lt;/h3&gt;

&lt;p&gt;If your player uses a Rigidbody component, it's better to handle physics movement inside FixedUpdate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerPhysicsMovement&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Rigidbody&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetAxis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Horizontal"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetAxis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Vertical"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;FixedUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;newPosition&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
            &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;movement&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fixedDeltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MovePosition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;newPosition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, I read player input inside Update and move the Rigidbody inside FixedUpdate.&lt;/p&gt;

&lt;p&gt;This is a useful approach because input handling and physics calculations have different update requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My approach:&lt;/strong&gt; Whenever I'm working with Rigidbody physics, I prefer FixedUpdate for physics operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Unity Update vs FixedUpdate: Main Differences
&lt;/h2&gt;

&lt;p&gt;Let's understand the main differences between Update and FixedUpdate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7a38xbqufkc38v9s46wd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7a38xbqufkc38v9s46wd.png" alt="differences between Update and FixedUpdate" width="799" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; FixedUpdate doesn't always run once per frame. Depending on the frame rate, Unity may call it multiple times or skip it during a particular rendered frame.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. What Is LateUpdate() in Unity?
&lt;/h2&gt;

&lt;p&gt;Apart from Update and FixedUpdate, Unity also provides &lt;code&gt;LateUpdate()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;LateUpdate runs after all Update methods have finished for the frame.&lt;/p&gt;

&lt;p&gt;I mainly use it when one object needs to follow another object after its movement is complete.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Camera Follow Using LateUpdate()
&lt;/h3&gt;

&lt;p&gt;Imagine a player moving forward and a camera following that player.&lt;/p&gt;

&lt;p&gt;If both the player and camera update their positions independently inside Update, their execution order can sometimes cause camera jitter.&lt;/p&gt;

&lt;p&gt;For a simple transform-based player movement system, LateUpdate is a good choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CameraFollow&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Transform&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;LateUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The player updates its position first, and then the camera follows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My approach:&lt;/strong&gt; I use LateUpdate for camera follow systems and other operations that depend on objects updated during Update.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Common Mistakes I Avoid in Unity
&lt;/h2&gt;

&lt;p&gt;Understanding these methods is important, but knowing what to avoid is equally important.&lt;/p&gt;

&lt;p&gt;Here are some common mistakes I try to avoid in my Unity projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Moving Rigidbody Objects Using Transform
&lt;/h3&gt;

&lt;p&gt;Avoid directly changing the Transform position when you want an object to behave according to Rigidbody physics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, use Rigidbody methods such as &lt;code&gt;MovePosition()&lt;/code&gt; inside FixedUpdate for kinematic movement.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Forgetting Time.deltaTime
&lt;/h3&gt;

&lt;p&gt;Without Time.deltaTime, movement speed depends on the frame rate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Avoid&lt;/span&gt;
&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Better&lt;/span&gt;
&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forward&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Reading Input Only Inside FixedUpdate
&lt;/h3&gt;

&lt;p&gt;I generally avoid reading player input only inside FixedUpdate.&lt;/p&gt;

&lt;p&gt;Input events can happen between physics updates, so some input events may be missed depending on the input system and configuration.&lt;/p&gt;

&lt;p&gt;Instead, capture input using Update or the appropriate Input System callbacks and pass it to your physics logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Calling Expensive Methods Every Frame
&lt;/h3&gt;

&lt;p&gt;Avoid repeatedly searching for objects inside Update when you can cache their references.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Avoid&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;FindObjectOfType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameManager&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Better approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="n"&gt;gameManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;gameManager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FindFirstObjectByType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameManager&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameManager&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Use cached reference&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This avoids performing the same object search every frame.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Using FixedUpdate for Everything
&lt;/h3&gt;

&lt;p&gt;FixedUpdate is not a performance optimization method for all gameplay logic.&lt;/p&gt;

&lt;p&gt;It is designed for physics-related operations. Moving UI updates, regular gameplay checks, and unrelated calculations into FixedUpdate doesn't automatically make your game faster.&lt;/p&gt;

&lt;p&gt;Always choose the method based on what the code needs to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. My Practical Approach: Which Method Do I Use?
&lt;/h2&gt;

&lt;p&gt;Here is a simple reference that you can follow while developing your Unity games.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fueycleatla37tjx1kmj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fueycleatla37tjx1kmj8.png" alt="which method to choose when - Unity Update vs FixedUpdate" width="800" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember that these are general guidelines. Some situations require a different approach depending on your game's architecture and requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Unity Update vs FixedUpdate
&lt;/h2&gt;

&lt;p&gt;Choosing between Update, FixedUpdate, and LateUpdate becomes easier once you understand their purpose.&lt;/p&gt;

&lt;p&gt;Here is a quick summary of what I recommend remembering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Update():&lt;/strong&gt; Use it for player input, UI updates, and regular gameplay logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FixedUpdate():&lt;/strong&gt; Use it for Rigidbody movement, applying forces, and physics calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LateUpdate():&lt;/strong&gt; Use it for camera follow and operations that should happen after Update.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time.deltaTime:&lt;/strong&gt; Use it for frame-rate-independent movement and time-based calculations in Update.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time.fixedDeltaTime:&lt;/strong&gt; Use it for time-based calculations inside FixedUpdate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache references:&lt;/strong&gt; Avoid repeatedly searching for objects inside Update.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't mix physics and Transform movement:&lt;/strong&gt; Choose the correct movement approach for your Rigidbody setup.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simple rule I follow is: &lt;strong&gt;Use Update for gameplay, FixedUpdate for physics, and LateUpdate when execution order matters.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Understanding these small differences can help you write cleaner scripts, avoid common physics problems, and build more reliable Unity games.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Thanks for reading!&lt;/strong&gt; ❤️&lt;/p&gt;

&lt;p&gt;I hope this article helped you understand the difference between Unity Update and FixedUpdate.&lt;/p&gt;

&lt;p&gt;If you found it useful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Give this article a like.&lt;/li&gt;
&lt;li&gt;🔁 Share it with other Unity developers.&lt;/li&gt;
&lt;li&gt;💬 Leave a comment and share how you use Update and FixedUpdate in your projects.&lt;/li&gt;
&lt;li&gt;⭐ Follow me for more Unity development tips, tutorials, and practical programming guides.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy Game Development! 🎮&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>csharp</category>
      <category>gamedev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Host Unity WebGL Games Online: A Beginner's Guide</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Fri, 20 Nov 2026 09:40:31 +0000</pubDate>
      <link>https://dev.to/alok_krishali/host-unity-webgl-games-online-a-beginners-guide-4ej3</link>
      <guid>https://dev.to/alok_krishali/host-unity-webgl-games-online-a-beginners-guide-4ej3</guid>
      <description>&lt;p&gt;Have you ever wanted to &lt;strong&gt;host Unity WebGL games&lt;/strong&gt; online so players can enjoy your game directly in their web browsers without downloading or installing anything? Good news! You don't need to build a complicated gaming platform or manage an expensive server to get started.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.unity3d.com/560/Documentation/Manual/webgl-gettingstarted.html" rel="noopener noreferrer"&gt;Unity WebGL&lt;/a&gt; allows developers to turn their Unity games into browser-based experiences that players can access through a simple website URL. Whether you've created a 2D puzzle game, a 3D adventure, or a multiplayer experience, you can publish it online and let people play it from their desktops and supported mobile browsers.&lt;/p&gt;

&lt;p&gt;In this beginner-friendly guide, I'll explain &lt;strong&gt;how to host Unity WebGL games online&lt;/strong&gt;, prepare your WebGL build, upload game files, choose a hosting platform, and make your game accessible through a website.&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Unity WebGL Hosting?
&lt;/h2&gt;

&lt;p&gt;Before we start hosting, let's understand what Unity WebGL hosting actually means.&lt;/p&gt;

&lt;p&gt;Unity WebGL is a Unity build platform that allows developers to run games inside compatible web browsers using web technologies.&lt;/p&gt;

&lt;p&gt;When you build a game for WebGL, Unity generates several files, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;index.html:&lt;/strong&gt; The webpage that loads your Unity game.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build folder:&lt;/strong&gt; Contains compiled game files, JavaScript, WebAssembly, and related resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TemplateData folder:&lt;/strong&gt; Contains supporting website assets such as loading screens and styles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StreamingAssets folder:&lt;/strong&gt; Contains additional files if your project uses them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Unity WebGL hosting&lt;/strong&gt; means uploading these generated files to a web server so visitors can open your game through a browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2ojvw140eam3md3bh54w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2ojvw140eam3md3bh54w.png" alt="Unity WebGL build files and folders" width="800" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://yourwebsite.com/games/my-unity-game/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When someone visits this URL, the browser loads your Unity WebGL build and displays your game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Prepare Your Unity WebGL Game Build
&lt;/h2&gt;

&lt;p&gt;The first step is to generate a WebGL build from your Unity project.&lt;/p&gt;

&lt;p&gt;Follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your Unity project.&lt;/li&gt;
&lt;li&gt;Go to &lt;strong&gt;File → Build Profiles&lt;/strong&gt; (or Build Settings in older Unity versions).&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Web&lt;/strong&gt; or &lt;strong&gt;WebGL&lt;/strong&gt;, depending on your Unity version.&lt;/li&gt;
&lt;li&gt;Switch your active build platform if necessary.&lt;/li&gt;
&lt;li&gt;Open Player Settings and configure your WebGL settings.&lt;/li&gt;
&lt;li&gt;Select your preferred compression format.&lt;/li&gt;
&lt;li&gt;Click Build and choose an output folder.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fts7cpq4y30b95s6y7gcq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fts7cpq4y30b95s6y7gcq.jpg" alt="Setup unity webGL build and player setting" width="800" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, you can name your output folder &lt;code&gt;MyGame_WebGL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After the build completes, Unity generates the necessary files for your browser game.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Make sure you test your WebGL build locally using a local web server rather than simply opening the &lt;code&gt;index.html&lt;/code&gt; file directly. Some browser features require HTTP or HTTPS to work correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Choose a Platform to Host Unity WebGL Games
&lt;/h2&gt;

&lt;p&gt;Now that your build is ready, you need a hosting platform.&lt;/p&gt;

&lt;p&gt;There are several ways to &lt;strong&gt;host Unity WebGL games online&lt;/strong&gt;. Your choice depends on your budget, website requirements, expected traffic, and technical knowledge.&lt;/p&gt;

&lt;p&gt;Here are some beginner-friendly options.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. GitHub Pages
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; Pages is a convenient option for hosting static websites and smaller Unity WebGL projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Free hosting for eligible public repositories.&lt;/li&gt;
&lt;li&gt;Supports custom domains.&lt;/li&gt;
&lt;li&gt;HTTPS support.&lt;/li&gt;
&lt;li&gt;Easy integration with GitHub repositories.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to use it:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a GitHub repository.&lt;/li&gt;
&lt;li&gt;Upload your Unity WebGL build files.&lt;/li&gt;
&lt;li&gt;Make sure &lt;code&gt;index.html&lt;/code&gt; is in the published directory.&lt;/li&gt;
&lt;li&gt;Open your repository settings.&lt;/li&gt;
&lt;li&gt;Find the Pages section.&lt;/li&gt;
&lt;li&gt;Select your deployment branch and folder.&lt;/li&gt;
&lt;li&gt;Save your settings and wait for deployment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once published, GitHub provides a website URL where visitors can access your game.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Check GitHub Pages' file-size and bandwidth limits before uploading larger Unity builds. It is not ideal for every large game project.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Netlify
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.netlify.com/" rel="noopener noreferrer"&gt;https://www.netlify.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Netlify is another beginner-friendly option for hosting static websites and &lt;a href="https://learngamestutorial.com/unity-webgl-games/" rel="noopener noreferrer"&gt;Unity WebGL games&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can deploy your build without managing a traditional web server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to use it:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Visit Netlify and create an account.&lt;/li&gt;
&lt;li&gt;Prepare your Unity WebGL build folder.&lt;/li&gt;
&lt;li&gt;Upload your published website folder using Netlify's supported deployment method.&lt;/li&gt;
&lt;li&gt;Wait for the deployment process.&lt;/li&gt;
&lt;li&gt;Open the generated website URL.&lt;/li&gt;
&lt;li&gt;Test your game in a browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Netlify also supports custom domains and HTTPS.&lt;/p&gt;

&lt;p&gt;One important thing to remember is that Unity WebGL builds may require specific HTTP headers for compressed files. You can configure these through Netlify's supported headers configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. itch.io
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://itch.io/" rel="noopener noreferrer"&gt;https://itch.io/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If your main goal is to publish a browser-playable game rather than integrate it into your own website, itch.io is worth considering.&lt;/p&gt;

&lt;p&gt;It is a popular platform for independent game developers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to publish your Unity WebGL game:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build your game for WebGL.&lt;/li&gt;
&lt;li&gt;Compress the generated build files into a ZIP archive.&lt;/li&gt;
&lt;li&gt;Create a new project on itch.io.&lt;/li&gt;
&lt;li&gt;Select HTML as the project type.&lt;/li&gt;
&lt;li&gt;Upload your ZIP file.&lt;/li&gt;
&lt;li&gt;Enable the option to run the game in the browser.&lt;/li&gt;
&lt;li&gt;Save and publish your project.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Players can now visit your itch.io game page and play your Unity game directly in their browsers.&lt;/p&gt;

&lt;p&gt;This is particularly useful for sharing prototypes, demos, and indie games.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Your Own Website or Web Hosting
&lt;/h3&gt;

&lt;p&gt;If you already own a website, you can &lt;strong&gt;host Unity WebGL games on your website&lt;/strong&gt; without creating a separate gaming platform.&lt;/p&gt;

&lt;p&gt;For example, if you have a WordPress website or a domain with shared hosting, you can upload your Unity WebGL build into a dedicated folder.&lt;/p&gt;

&lt;p&gt;Let's say your website is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://example.com/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can create a folder:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://example.com/games/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then upload your game into another folder:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://example.com/games/my-game/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your final game URL will be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://example.com/games/my-game/index.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This approach gives you more control over your game presentation, branding, website traffic, and monetization.&lt;/p&gt;

&lt;p&gt;However, make sure your hosting provider supports the requirements of Unity WebGL builds, including the necessary MIME types, compression, and HTTP headers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Upload Your Unity WebGL Build to Your Website
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzir4y83aqj70blg94asi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzir4y83aqj70blg94asi.png" alt="WordPress File manager view for Uploading webGL build" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you choose your own hosting or a web hosting provider, the next step is uploading your game files.&lt;/p&gt;

&lt;p&gt;You can use an FTP client such as FileZilla or your hosting provider's file manager.&lt;/p&gt;

&lt;p&gt;Follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Log in to your hosting control panel.&lt;/li&gt;
&lt;li&gt;Open File Manager or connect using FTP.&lt;/li&gt;
&lt;li&gt;Navigate to your website's public directory, commonly called &lt;code&gt;public_html&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a folder named &lt;code&gt;games&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create another folder using your game name.&lt;/li&gt;
&lt;li&gt;Upload all the generated Unity WebGL build files.&lt;/li&gt;
&lt;li&gt;Maintain the original folder structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdflgsr51pd27555rwcbq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdflgsr51pd27555rwcbq.png" alt="File manager FTP client view" width="799" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your directory should look similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public_html/
│
└── games/
    │
    └── my-game/
        │
        ├── index.html
        │
        ├── Build/
        │   ├── MyGame.loader.js
        │   ├── MyGame.framework.js
        │   ├── MyGame.data
        │   └── MyGame.wasm
        │
        └── TemplateData/
            ├── style.css
            └── favicon.ico
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: The exact generated files and folder names depend on your Unity version, build settings, and project configuration.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After uploading, open your game URL:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;https://example.com/games/my-game/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If everything is configured correctly, your Unity game should start loading in the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Configure WebGL Compression and Server Settings
&lt;/h2&gt;

&lt;p&gt;This is one of the most important steps when you &lt;strong&gt;publish Unity WebGL games&lt;/strong&gt; online.&lt;/p&gt;

&lt;p&gt;Sometimes developers upload their game files successfully but see errors such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unable to parse Build file.&lt;/li&gt;
&lt;li&gt;Failed to download &lt;code&gt;.wasm&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Incorrect Content-Encoding.&lt;/li&gt;
&lt;li&gt;Unable to load Unity framework.&lt;/li&gt;
&lt;li&gt;Brotli or Gzip decompression errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These problems can happen when the hosting server does not correctly serve Unity's compressed build files.&lt;/p&gt;

&lt;p&gt;Unity supports compression options such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Gzip:&lt;/strong&gt; Compresses files to reduce download size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Brotli:&lt;/strong&gt; Often provides better compression than Gzip.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disabled:&lt;/strong&gt; Generates uncompressed files, which can simplify hosting but increase download sizes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you use Gzip or Brotli, your server must serve the corresponding files with the correct HTTP response headers.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;File type&lt;/th&gt;
&lt;th&gt;Important response header&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.wasm&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Content-Type: application/wasm&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.js&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Content-Type: application/javascript&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;.data&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Content-Type: application/octet-stream&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For precompressed files, the server must also return the appropriate &lt;code&gt;Content-Encoding&lt;/code&gt; header, such as &lt;code&gt;gzip&lt;/code&gt; or &lt;code&gt;br&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Content-Encoding: gzip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Content-Encoding: br
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The correct configuration depends on your Unity compression settings and the actual files generated by your build.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My suggestion:&lt;/strong&gt; If you are a beginner and your hosting provider does not support custom HTTP headers, consider using an uncompressed build initially. Once your game works, configure compression properly to improve loading performance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flymq84ysmpoaaj69w0ro.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flymq84ysmpoaaj69w0ro.jpg" alt="Unity webGL build compression format and setting" width="800" height="798"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Test Your Unity WebGL Game in a Browser
&lt;/h2&gt;

&lt;p&gt;Uploading your files is not the final step. You should test your game before sharing it with players.&lt;/p&gt;

&lt;p&gt;Open your published game URL in Chrome, Firefox, or another supported browser.&lt;/p&gt;

&lt;p&gt;Check the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the Unity loading screen appear?&lt;/li&gt;
&lt;li&gt;Does the game start without errors?&lt;/li&gt;
&lt;li&gt;Are all textures, audio files, and assets loading?&lt;/li&gt;
&lt;li&gt;Are keyboard and mouse controls working?&lt;/li&gt;
&lt;li&gt;Does the game fit different screen sizes?&lt;/li&gt;
&lt;li&gt;Are there any errors in the browser console?&lt;/li&gt;
&lt;li&gt;Does the game work over HTTPS?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can open browser developer tools by pressing &lt;code&gt;F12&lt;/code&gt; and checking the Console and Network tabs.&lt;/p&gt;

&lt;p&gt;If your game does not load, check the file paths, server response headers, compression settings, and browser console errors.&lt;/p&gt;

&lt;p&gt;Testing on different browsers and devices will also help you identify compatibility and performance problems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flsd7bro0beuhic7hjbsl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flsd7bro0beuhic7hjbsl.png" alt="After Hosting webGL files on server - Build running" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: How to Host Unity WebGL Games Online
&lt;/h2&gt;

&lt;p&gt;Hosting a Unity WebGL game is easier when you follow the correct steps. You don't need an advanced server setup to publish your first browser game.&lt;/p&gt;

&lt;p&gt;Here is a quick summary of the complete process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a WebGL build:&lt;/strong&gt; Switch your Unity project to the Web platform and generate the required files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose a hosting platform:&lt;/strong&gt; Select GitHub Pages, Netlify, itch.io, or your own website.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Upload your game files:&lt;/strong&gt; Transfer the complete build while maintaining its original folder structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configure server settings:&lt;/strong&gt; Set the correct MIME types and compression headers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test your game:&lt;/strong&gt; Open the published URL and check loading, controls, assets, and browser compatibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Share your game:&lt;/strong&gt; Once everything works, share your game URL with players.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it! You have successfully learned the basic process of how to &lt;strong&gt;host Unity WebGL games online&lt;/strong&gt; and make your Unity project playable through a web browser.&lt;/p&gt;

&lt;p&gt;Have you hosted a Unity WebGL game before? Which hosting platform do you prefer?&lt;/p&gt;

&lt;p&gt;If you have any questions, suggestions, or concerns about this article or &lt;a href="https://unity.com/" rel="noopener noreferrer"&gt;unity&lt;/a&gt;, feel free to ask in the comments. I'd love to hear about your experience.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, please &lt;strong&gt;like, share, and appreciate the post&lt;/strong&gt; so more Unity developers can discover it.&lt;/p&gt;

&lt;p&gt;Happy Game Development! 🎮&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>webdev</category>
      <category>webgl</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>Unity Character Controller Tutorial: Create Smooth Player Movement with C#</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Mon, 05 Oct 2026 11:52:37 +0000</pubDate>
      <link>https://dev.to/alok_krishali/unity-character-controller-tutorial-create-smooth-player-movement-with-c-4cb8</link>
      <guid>https://dev.to/alok_krishali/unity-character-controller-tutorial-create-smooth-player-movement-with-c-4cb8</guid>
      <description>&lt;p&gt;&lt;strong&gt;Learn Character Controller in unity for Player Movement&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Your character can move. Great! But what happens when it reaches a wall? What about gravity, slopes, or jumping?&lt;/p&gt;

&lt;p&gt;If you're building a 3D Unity game, simply changing the player's &lt;code&gt;Transform&lt;/code&gt; isn't always enough. For a more reliable character controller, &lt;a href="https://unity.com/" rel="noopener noreferrer"&gt;Unity &lt;/a&gt;provides the &lt;strong&gt;CharacterController&lt;/strong&gt; component.&lt;/p&gt;

&lt;p&gt;In this beginner-friendly tutorial, we'll use &lt;code&gt;CharacterController&lt;/code&gt; to create a simple player controller with &lt;strong&gt;movement, collision detection, gravity, and jumping&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is CharacterController in Unity?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;CharacterController&lt;/code&gt; is a Unity component designed for controlling characters without using a Rigidbody for their basic movement.&lt;/p&gt;

&lt;p&gt;It can help your player:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move around the environment&lt;/li&gt;
&lt;li&gt;Collide with walls and other objects&lt;/li&gt;
&lt;li&gt;Stay on the ground&lt;/li&gt;
&lt;li&gt;Handle slopes and steps&lt;/li&gt;
&lt;li&gt;Move without passing through colliders&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's commonly used for first-person and third-person player controllers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; CharacterController is not a physics Rigidbody. If you need your player to behave like a physics object, a Rigidbody may be a better choice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 1: Create a Player
&lt;/h2&gt;

&lt;p&gt;For testing, let's create a simple Capsule.&lt;/p&gt;

&lt;p&gt;In Unity:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hierarchy → Right-click → 3D Object → Capsule&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rename it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Player
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set its position to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X = 0
Y = 1
Z = 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can later replace the Capsule with your actual character model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Add CharacterController
&lt;/h2&gt;

&lt;p&gt;Select your &lt;code&gt;Player&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;In the Inspector, select:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Component → Character Controller&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Unity will add a CharacterController component.&lt;/p&gt;

&lt;p&gt;You should see properties such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Center&lt;/li&gt;
&lt;li&gt;Radius&lt;/li&gt;
&lt;li&gt;Height&lt;/li&gt;
&lt;li&gt;Slope Limit&lt;/li&gt;
&lt;li&gt;Step Offset&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a Capsule player, Unity's default values are usually a good starting point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create the Player Movement Script
&lt;/h2&gt;

&lt;p&gt;Create a new C# script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PlayerController
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open it and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;moveSpeed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;9.81f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;jumpHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;CharacterController&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GetComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CharacterController&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;horizontal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetAxisRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Horizontal"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetAxisRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Vertical"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;moveSpeed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetButtonDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Jump"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jumpHeight&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Attach the script to your &lt;strong&gt;Player&lt;/strong&gt; GameObject.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Test Player Movement
&lt;/h2&gt;

&lt;p&gt;Press &lt;strong&gt;Play&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can now use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;W / Up Arrow&lt;/strong&gt; → Move forward&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;S / Down Arrow&lt;/strong&gt; → Move backward&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A / Left Arrow&lt;/strong&gt; → Move left&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D / Right Arrow&lt;/strong&gt; → Move right&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space&lt;/strong&gt; → Jump&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The CharacterController will handle collisions with objects that have colliders.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does CharacterController.Move() Work?
&lt;/h2&gt;

&lt;p&gt;The main movement happens here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;moveSpeed&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of directly changing the player's transform, we're asking the CharacterController to move the character.&lt;/p&gt;

&lt;p&gt;This is important because the controller can detect collisions while moving.&lt;/p&gt;

&lt;p&gt;For example, if your player walks toward a wall with a Collider, the CharacterController won't simply move through it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Use .normalized?
&lt;/h3&gt;

&lt;p&gt;We create our movement direction with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;horizontal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vertical&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;normalized&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without normalization, moving diagonally could make the player move faster than moving in one direction.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.normalized&lt;/code&gt; keeps the direction's length consistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Gravity
&lt;/h2&gt;

&lt;p&gt;One common beginner mistake is assuming that CharacterController automatically applies gravity.&lt;/p&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;p&gt;That's why we create:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Vector3&lt;/span&gt; &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then apply gravity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we move the controller vertically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Move&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;velocity&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the character natural downward movement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping the Player on the Ground
&lt;/h2&gt;

&lt;p&gt;We use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;controller.isGrounded&lt;/code&gt; tells us whether the character is touching the ground.&lt;/p&gt;

&lt;p&gt;Setting a small negative value instead of &lt;code&gt;0&lt;/code&gt; helps keep the controller properly grounded.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Jumping
&lt;/h2&gt;

&lt;p&gt;The jump happens here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetButtonDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Jump"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;velocity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jumpHeight&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2f&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;gravity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part is the &lt;code&gt;controller.isGrounded&lt;/code&gt; check.&lt;/p&gt;

&lt;p&gt;It prevents the player from continuously jumping while already in the air.&lt;/p&gt;

&lt;p&gt;You can make the jump higher by increasing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;jumpHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1.0 → Small jump
1.5 → Normal jump
3.0 → High jump
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Make Sure Your Floor Has a Collider
&lt;/h2&gt;

&lt;p&gt;Your CharacterController needs something to collide with.&lt;/p&gt;

&lt;p&gt;For example, create a Plane:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hierarchy → 3D Object → Plane&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Unity Plane already has a &lt;code&gt;MeshCollider&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can also use cubes as floors and walls. A Cube has a &lt;code&gt;BoxCollider&lt;/code&gt; by default.&lt;/p&gt;

&lt;p&gt;Your scene might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Scene
├── Main Camera
├── Directional Light
├── Player
│   ├── CharacterController
│   └── PlayerController
├── Ground
│   └── Collider
└── Wall
    └── Collider
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CharacterController vs Rigidbody
&lt;/h2&gt;

&lt;p&gt;Both approaches can be useful, but they solve slightly different problems.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;CharacterController&lt;/th&gt;
&lt;th&gt;Rigidbody&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Designed for character movement&lt;/td&gt;
&lt;td&gt;Physics-based&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Easy to control directly&lt;/td&gt;
&lt;td&gt;Uses physics simulation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Great for FPS/TPS characters&lt;/td&gt;
&lt;td&gt;Great for physics-driven characters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Handles character collisions&lt;/td&gt;
&lt;td&gt;Handles forces and physics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gravity must be coded&lt;/td&gt;
&lt;td&gt;Gravity can come from physics&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For a traditional player-controlled character, &lt;strong&gt;CharacterController is a good starting point&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Beginner Problems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Player Falls Through the Floor
&lt;/h3&gt;

&lt;p&gt;Make sure the floor has a Collider.&lt;/p&gt;

&lt;h3&gt;
  
  
  Player Doesn't Move
&lt;/h3&gt;

&lt;p&gt;Check that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;PlayerController&lt;/code&gt; is attached.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CharacterController&lt;/code&gt; is attached.&lt;/li&gt;
&lt;li&gt;You're pressing the correct movement keys.&lt;/li&gt;
&lt;li&gt;The GameObject is active.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Player Can Jump in the Air
&lt;/h3&gt;

&lt;p&gt;Make sure your jump code includes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isGrounded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Player Moves Too Fast
&lt;/h3&gt;

&lt;p&gt;Lower:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;moveSpeed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try &lt;code&gt;3f&lt;/code&gt; or &lt;code&gt;4f&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Can You Add Next?
&lt;/h2&gt;

&lt;p&gt;Once this basic controller works, you can expand it with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sprinting&lt;/li&gt;
&lt;li&gt;Crouching&lt;/li&gt;
&lt;li&gt;Smooth character rotation&lt;/li&gt;
&lt;li&gt;First-person camera&lt;/li&gt;
&lt;li&gt;Third-person camera&lt;/li&gt;
&lt;li&gt;Animation transitions&lt;/li&gt;
&lt;li&gt;Footstep sounds&lt;/li&gt;
&lt;li&gt;Stamina&lt;/li&gt;
&lt;li&gt;Mobile joystick controls&lt;/li&gt;
&lt;li&gt;Slope handling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important thing is to build these features one at a time.&lt;br&gt;
Check here - &lt;a href="https://learngamestutorial.com/" rel="noopener noreferrer"&gt;Unity game development tutorials&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;CharacterController&lt;/code&gt; gives you a solid starting point for building a 3D player in Unity. Instead of simply moving the Transform, you get a controller designed specifically for character movement and collision handling.&lt;/p&gt;

&lt;p&gt;Start with basic movement, then add gravity and jumping. Once you understand how &lt;code&gt;CharacterController.Move()&lt;/code&gt;, &lt;code&gt;isGrounded&lt;/code&gt;, and velocity work together, you'll have a much better foundation for creating more advanced player controllers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Did you use CharacterController or Rigidbody in your first Unity game? Share your experience in the comments!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>gamedev</category>
      <category>programming</category>
      <category>csharp</category>
    </item>
    <item>
      <title>5 Common Mistakes When Using OnEnable() and OnDisable() in Unity</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Wed, 30 Sep 2026 05:50:39 +0000</pubDate>
      <link>https://dev.to/alok_krishali/5-common-mistakes-when-using-onenable-and-ondisable-in-unity-21l5</link>
      <guid>https://dev.to/alok_krishali/5-common-mistakes-when-using-onenable-and-ondisable-in-unity-21l5</guid>
      <description>&lt;p&gt;Unity's &lt;code&gt;OnEnable()&lt;/code&gt; and &lt;code&gt;OnDisable()&lt;/code&gt; methods look simple, but they can cause some surprisingly subtle bugs.&lt;/p&gt;

&lt;p&gt;They are commonly used to subscribe and unsubscribe from events, start or stop listeners, refresh UI, and manage temporary behaviour. The catch is that these methods are tied to a component's &lt;strong&gt;enabled state&lt;/strong&gt;, not simply to its creation or destruction.&lt;/p&gt;

&lt;p&gt;Understanding exactly when they run can save you from duplicate event calls, memory leaks, unexpected behaviour, and null-reference exceptions.&lt;/p&gt;

&lt;p&gt;Let's look at five common mistakes and how to avoid them.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Forgetting to Unsubscribe from Events
&lt;/h2&gt;

&lt;p&gt;One of the most common patterns is subscribing to an event in &lt;code&gt;OnEnable()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;GameManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnGameOver&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HandleGameOver&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you subscribe without unsubscribing, the event publisher can continue holding a reference to your component.&lt;/p&gt;

&lt;p&gt;The matching cleanup should happen in &lt;code&gt;OnDisable()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;GameManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnGameOver&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HandleGameOver&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;GameManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnGameOver&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;HandleGameOver&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HandleGameOver&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Game Over!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why does this matter?
&lt;/h3&gt;

&lt;p&gt;Imagine a UI panel that can be enabled and disabled multiple times. If every &lt;code&gt;OnEnable()&lt;/code&gt; adds another subscription, the same callback may eventually be invoked multiple times.&lt;/p&gt;

&lt;p&gt;Even worse, if the publisher lives longer than the subscriber, failing to unsubscribe can keep references around longer than expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  A useful rule
&lt;/h3&gt;

&lt;p&gt;If you subscribe in &lt;code&gt;OnEnable()&lt;/code&gt;, the safest default is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Unsubscribe from the same event in &lt;code&gt;OnDisable()&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This keeps the subscription tied to the component's active state.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Assuming &lt;code&gt;OnEnable()&lt;/code&gt; Runs Only Once
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;OnEnable()&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; mean "run once when this object is created."&lt;/p&gt;

&lt;p&gt;It runs whenever the component becomes enabled and active.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enabled!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;OnEnable()&lt;/code&gt; can run each time the object becomes active again.&lt;/p&gt;

&lt;p&gt;The same principle applies when changing a component's &lt;code&gt;enabled&lt;/code&gt; property:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;myComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;myComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;enabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can trigger &lt;code&gt;OnDisable()&lt;/code&gt; and &lt;code&gt;OnEnable()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this important?
&lt;/h3&gt;

&lt;p&gt;Code like this can become problematic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;LoadData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;CreateSomething&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;StartListening&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you expect all of this to happen only once, disabling and re-enabling the component may repeat those operations.&lt;/p&gt;

&lt;p&gt;For one-time initialization, consider using &lt;code&gt;Awake()&lt;/code&gt; or &lt;code&gt;Start()&lt;/code&gt; when appropriate.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Initialize&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;StartListening&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;StopListening&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important distinction is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Awake()&lt;/code&gt; → initialization&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Start()&lt;/code&gt; → initialization that can wait until the first frame&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;OnEnable()&lt;/code&gt; → happens whenever the component becomes enabled and active&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;OnDisable()&lt;/code&gt; → happens whenever the component becomes disabled or inactive&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Confusing Disabled Components with Destroyed Objects
&lt;/h2&gt;

&lt;p&gt;Disabling an object does not destroy it.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;GameObject&lt;/code&gt; still exists. Its components still exist. Its fields still contain their values.&lt;/p&gt;

&lt;p&gt;The component simply isn't active.&lt;/p&gt;

&lt;p&gt;That means this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Object disabled"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;does &lt;strong&gt;not&lt;/strong&gt; mean:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This object is being destroyed."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This component is no longer enabled and active."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An object can later become active again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;OnEnable()&lt;/code&gt; will run again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does this distinction matter?
&lt;/h3&gt;

&lt;p&gt;Suppose you clear important state in &lt;code&gt;OnDisable()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;currentTarget&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might assume the object is going away permanently.&lt;/p&gt;

&lt;p&gt;But if another system re-enables it, the component now has to deal with that cleared state.&lt;/p&gt;

&lt;p&gt;For cleanup that should happen specifically when an object is destroyed, &lt;code&gt;OnDestroy()&lt;/code&gt; may be more appropriate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Final cleanup before the object is destroyed.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The lifecycle methods serve different purposes. Don't treat &lt;code&gt;OnDisable()&lt;/code&gt; as a synonym for &lt;code&gt;OnDestroy()&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Unexpected Repeated Subscriptions
&lt;/h2&gt;

&lt;p&gt;This mistake is closely related to the first one, but it deserves separate attention.&lt;/p&gt;

&lt;p&gt;Consider:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens if you forget the corresponding unsubscribe?&lt;/p&gt;

&lt;p&gt;Every enable adds another subscription:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enable → Subscribe
Disable
Enable → Subscribe again
Disable
Enable → Subscribe again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eventually, one event can produce multiple calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OnScoreChanged
    ↓
UpdateScore()
UpdateScore()
UpdateScore()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can lead to confusing bugs such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI updating multiple times&lt;/li&gt;
&lt;li&gt;Audio playing repeatedly&lt;/li&gt;
&lt;li&gt;A method being executed several times&lt;/li&gt;
&lt;li&gt;Performance gradually getting worse&lt;/li&gt;
&lt;li&gt;State changing unexpectedly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The basic fix is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  But what if the event is only supposed to be subscribed once?
&lt;/h3&gt;

&lt;p&gt;Then &lt;code&gt;OnEnable()&lt;/code&gt; may not be the right place.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDestroy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;EventManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pattern ties the subscription to the object's lifetime rather than its enabled state.&lt;/p&gt;

&lt;p&gt;The right approach depends on what you want:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subscribe while enabled?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[OnEnable  ](https://learngamestutorial.com/unity-onenable-function-us/)→ Subscribe
OnDisable → Unsubscribe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Subscribe for the object's lifetime?&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Awake     → Subscribe
OnDestroy → Unsubscribe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Choose the lifecycle that matches the actual requirement.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Null-Reference Problems
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;OnEnable()&lt;/code&gt; can run earlier than you expect, especially when a dependency hasn't been initialized yet.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;UIManager&lt;/span&gt; &lt;span class="n"&gt;uiManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;uiManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Refresh&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;uiManager&lt;/code&gt; hasn't been assigned, this can result in a &lt;code&gt;NullReferenceException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same problem can happen when an object referenced by the component has already been destroyed or isn't available during a particular lifecycle transition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid assuming dependencies always exist
&lt;/h3&gt;

&lt;p&gt;If a reference is required, make that requirement explicit.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;UIManager&lt;/span&gt; &lt;span class="n"&gt;uiManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uiManager&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"UIManager is not assigned."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then keep &lt;code&gt;OnEnable()&lt;/code&gt; focused on the work that should happen when the component becomes active:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uiManager&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;uiManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Refresh&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, don't blindly add null checks everywhere. A null check can hide an actual setup problem.&lt;/p&gt;

&lt;p&gt;If a dependency is mandatory, failing loudly during initialization can make the bug much easier to find.&lt;/p&gt;




&lt;h3&gt;
  
  
  A Practical Pattern
&lt;/h3&gt;

&lt;p&gt;For many event-driven components, this is a good starting pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ScoreDisplay&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ScoreManager&lt;/span&gt; &lt;span class="n"&gt;scoreManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scoreManager&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ScoreManager is not assigned."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnEnable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scoreManager&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;scoreManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnDisable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scoreManager&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;scoreManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnScoreChanged&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;UpdateScore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Score: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, initialization happens in &lt;code&gt;Awake()&lt;/code&gt;, while the event subscription follows the component's enabled state.&lt;/p&gt;

&lt;p&gt;When the component is disabled, it stops listening. When it becomes enabled again, it subscribes again.&lt;/p&gt;




&lt;h3&gt;
  
  
  The Key Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;OnEnable()&lt;/code&gt; and &lt;code&gt;OnDisable()&lt;/code&gt; are primarily &lt;strong&gt;state-transition callbacks&lt;/strong&gt;, not one-time initialization and destruction callbacks.&lt;/p&gt;

&lt;p&gt;When working with them, ask yourself:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Can this component be enabled more than once?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Does every subscription have a matching unsubscribe?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Do I need cleanup on disable, or only on destruction?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Could this dependency be null when &lt;code&gt;OnEnable()&lt;/code&gt; runs?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Should this code run every time the component becomes active?&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you keep those questions in mind, many of the most common lifecycle bugs in Unity become much easier to prevent.&lt;/p&gt;

&lt;p&gt;A simple mental model is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Awake
  ↓
Start
  ↓
OnEnable ←────────────┐
  ↓                   │
Component is active   │
  ↓                   │
OnDisable ────────────┘
  ↓
OnDestroy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Treat &lt;code&gt;OnEnable()&lt;/code&gt; and &lt;code&gt;OnDisable()&lt;/code&gt; as a pair whenever you're managing temporary subscriptions or listeners, and your components will be much easier to reason about.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-onenable-function-us/" rel="noopener noreferrer"&gt;Understanding Unity OnEnable method&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If you like the article, Please like and share&lt;/strong&gt; your valuable feedback. If something is not clear feel free to ask in comment.&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>gamedev</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Unity Object Pooling: A Complete Guide to Better Game Performance</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Tue, 15 Sep 2026 00:09:00 +0000</pubDate>
      <link>https://dev.to/alok_krishali/unity-object-pooling-how-to-use-object-pool-in-your-game-374j</link>
      <guid>https://dev.to/alok_krishali/unity-object-pooling-how-to-use-object-pool-in-your-game-374j</guid>
      <description>&lt;p&gt;&lt;strong&gt;Unity Object Pooling&lt;/strong&gt; is a performance optimization technique in Unity that helps reduce unnecessary object creation and destruction during gameplay. Instead of repeatedly creating new GameObjects with &lt;code&gt;Instantiate()&lt;/code&gt; and removing them with &lt;code&gt;Destroy()&lt;/code&gt;, you can create objects in advance and reuse them whenever needed.&lt;/p&gt;

&lt;p&gt;For example, bullets, enemies, coins, explosions, and particle effects are frequently created and destroyed in many games. When hundreds of these objects are generated during gameplay, repeated instantiation and destruction can introduce performance overhead and garbage collection pressure.&lt;/p&gt;

&lt;p&gt;This is where object pooling becomes useful. In my Unity development experience, reusing existing GameObjects is a practical way to manage frequently spawned objects more efficiently.&lt;/p&gt;

&lt;p&gt;In this tutorial, you'll learn how &lt;a href="https://learngamestutorial.com/unity-object-pooling/" rel="noopener noreferrer"&gt;&lt;strong&gt;Unity Object Pooling&lt;/strong&gt;&lt;/a&gt; works, how to create a simple object pool using C#, and how to reuse GameObjects with a practical bullet example. I'll also explain when to use object pooling and how to reset pooled objects correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Unity Object Pooling?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Unity Object Pooling&lt;/strong&gt; is a technique where we create objects once and reuse them instead of constantly creating and destroying them.&lt;/p&gt;

&lt;p&gt;For example, without pooling, a shooting game may work like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shoot
 ↓
Instantiate Bullet
 ↓
Bullet Hits Enemy
 ↓
Destroy Bullet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens again every time the player shoots.&lt;/p&gt;

&lt;p&gt;With object pooling, the process becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Create Bullets
 ↓
Store Them in Pool
 ↓
Get Bullet
 ↓
Use Bullet
 ↓
Return Bullet to Pool
 ↓
Reuse Bullet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An object pool usually maintains a collection of inactive GameObjects. When the game needs an object, it retrieves one from the collection and activates it. Once the object is no longer needed, it is deactivated and returned to the pool for future use.&lt;/p&gt;

&lt;p&gt;The main idea is simple: &lt;strong&gt;reuse objects instead of creating new ones every time.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Object Pooling in Unity?
&lt;/h2&gt;

&lt;p&gt;Object pooling is useful when your game creates and removes the same objects frequently.&lt;/p&gt;

&lt;p&gt;Some common examples are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bullets&lt;/li&gt;
&lt;li&gt;Enemies&lt;/li&gt;
&lt;li&gt;Coins&lt;/li&gt;
&lt;li&gt;Explosions&lt;/li&gt;
&lt;li&gt;Particle effects&lt;/li&gt;
&lt;li&gt;Projectiles&lt;/li&gt;
&lt;li&gt;Damage numbers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can reduce the amount of repeated &lt;code&gt;Instantiate()&lt;/code&gt; and &lt;code&gt;Destroy()&lt;/code&gt; calls and help avoid unnecessary performance overhead, especially when many objects are involved.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of Unity Object Pooling
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Reduces repeated object instantiation.&lt;/strong&gt;&lt;br&gt;
Calling Instantiate() repeatedly—especially for bullets, enemies, or particle effects—takes CPU time and can cause frame drops or stutters. Pooling creates objects ahead of time (or on demand) and simply activates/deactivates them when needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Helps reduce garbage collection pressure.&lt;/strong&gt;&lt;br&gt;
Constantly creating and destroying objects generates memory allocations and dead references that Unity’s garbage collector eventually has to clean up. Reusing pooled objects keeps memory usage steadier and avoids sudden GC spikes during gameplay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Improves management of frequently spawned objects.&lt;/strong&gt;&lt;br&gt;
Instead of scattered Instantiate() and Destroy() calls across scripts, a pool gives you a single place to control active counts, reset object state (position, health, timers), and cap maximum instances so performance stays predictable.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to Use Object Pool in Unity
&lt;/h2&gt;

&lt;p&gt;Let's create a simple object pool for bullets.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Create a Bullet Prefab
&lt;/h3&gt;

&lt;p&gt;First, create your bullet GameObject and add the components you need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ig1zddqn8t6wm3inc36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4ig1zddqn8t6wm3inc36.png" alt="Bullet and Projectile Prefab" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bullet
 ├── Sprite Renderer
 ├── Collider2D
 └── Rigidbody2D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the bullet is ready, drag it from the Hierarchy into your Project folder to create a prefab.&lt;/p&gt;

&lt;p&gt;We will use this prefab to create our pool.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Create the Object Pool Script
&lt;/h3&gt;

&lt;p&gt;Create a C# script called &lt;code&gt;ObjectPool&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Collections&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ObjectPool&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;objectPrefab&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;poolSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;objectPool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Queue&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;poolSize&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;objectPrefab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="nc"&gt;GetObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dequeue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;ReturnObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry if the code looks a little new. The idea is quite simple.&lt;/p&gt;

&lt;p&gt;We create a number of objects when the game starts and keep them inside a &lt;code&gt;Queue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;poolSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;creates a pool with 30 objects.&lt;/p&gt;

&lt;p&gt;These objects are initially disabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They are now ready to be reused.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create the Pool in Unity
&lt;/h2&gt;

&lt;p&gt;Create an empty GameObject in your scene and rename it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BulletPool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Attach the &lt;code&gt;ObjectPool&lt;/code&gt; script to it.&lt;/p&gt;

&lt;p&gt;In the Inspector, you will see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object Prefab
Pool Size
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Drag your Bullet prefab into &lt;strong&gt;Object Prefab&lt;/strong&gt; and set the pool size to something like &lt;code&gt;30&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you press Play, Unity will create the bullets and keep them disabled.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Get an Object From the Pool
&lt;/h2&gt;

&lt;p&gt;Now we can use the pool when the player shoots.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ObjectPool&lt;/span&gt; &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Shoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bullet&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="c1"&gt;// Also can set rotation and Velocity to Zero Here&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of creating a new bullet with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bulletPrefab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we get an existing bullet from the pool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bullet is then activated and can be used normally.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Return the Object to the Pool
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Return the Bullet to the Pool after Hit&lt;/strong&gt; &lt;br&gt;
When bullet Hit Enemies In a shooting game, bullets should be returned to the object pool when they hit an enemy instead of being destroyed using Destroy(). We can use Unity's OnTriggerEnter2D() method to detect collisions and return the bullet to the pool.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bullet&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ObjectPool&lt;/span&gt; &lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;SetPool&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ObjectPool&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;objectPool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnTriggerEnter2D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collider2D&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CompareTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Enemy"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReturnObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;OnTriggerEnter2D()&lt;/code&gt; detects when the bullet enters another 2D collider.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CompareTag("Enemy")&lt;/code&gt; checks whether the bullet has hit an enemy.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ReturnObject(gameObject)&lt;/code&gt; deactivates the bullet and returns it to the pool for future reuse.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Make sure the bullet's Collider2D has Is Trigger enabled, the enemy has the appropriate Collider2D, and at least one object involved in the collision has a Rigidbody2D.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return Bullets When They Leave the Screen&lt;/strong&gt;&lt;br&gt;
Sometimes bullets don't hit any enemy and simply travel outside the camera view. Keeping these bullets active wastes available pooled objects.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;OnBecameInvisible()&lt;/code&gt; to return bullets when they are no longer visible to any camera:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnBecameInvisible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activeInHierarchy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;objectPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReturnObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is useful for simple 2D shooting games. However, remember that &lt;code&gt;OnBecameInvisible()&lt;/code&gt; is based on camera visibility, not actual screen boundaries. &lt;/p&gt;

&lt;p&gt;For more precise control, you can check the bullet's position against the camera's viewport bounds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Avoid returning a bullet multiple times. Your &lt;code&gt;ReturnObject()&lt;/code&gt; method should check whether the object is already inactive or already inside the pool before adding it back.&lt;/p&gt;

&lt;p&gt;When the bullet hits an enemy or leaves the screen, we don't destroy it.&lt;/p&gt;

&lt;p&gt;Instead, we return it to the pool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReturnObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The object is disabled and placed back into the pool.&lt;/p&gt;

&lt;p&gt;Later, when another bullet is needed, the same GameObject can be reused.&lt;/p&gt;

&lt;p&gt;This is the main benefit of &lt;strong&gt;Unity Object Pooling&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't Forget to Reset the Object
&lt;/h2&gt;

&lt;p&gt;There is one important thing to remember.&lt;/p&gt;

&lt;p&gt;A reused object may still have its previous state.&lt;/p&gt;

&lt;p&gt;For example, a bullet may still have its old:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Position&lt;/li&gt;
&lt;li&gt;Rotation&lt;/li&gt;
&lt;li&gt;Rigidbody velocity&lt;/li&gt;
&lt;li&gt;Animation state&lt;/li&gt;
&lt;li&gt;Health or other values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So make sure you reset anything that needs to be reset when the object is reused.&lt;/p&gt;

&lt;p&gt;For a Rigidbody2D bullet, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Rigidbody2D&lt;/span&gt; &lt;span class="n"&gt;rb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Rigidbody2D&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Velocity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Vector2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;rb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;angularVelocity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then set its new position and direction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unity's Built-in Object Pooling API
&lt;/h2&gt;

&lt;p&gt;Unity provides a built-in object pooling API through the &lt;code&gt;UnityEngine.Pool&lt;/code&gt; namespace. Instead of creating your own pooling system using a Queue or List, you can use the &lt;code&gt;ObjectPool&amp;lt;T&amp;gt;&lt;/code&gt; class to manage reusable objects more efficiently.&lt;/p&gt;

&lt;p&gt;The built-in &lt;code&gt;ObjectPool&amp;lt;T&amp;gt;&lt;/code&gt; API is available in modern Unity versions, including Unity 2021 LTS and later. It provides methods to retrieve objects, return them to the pool, and control how objects are created and reused.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use Unity's Built-in Object Pooling
&lt;/h3&gt;

&lt;p&gt;Let's create a simple bullet pooling system using Unity's built-in &lt;code&gt;ObjectPool&amp;lt;GameObject&amp;gt;&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Import the Required Namespace&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, import the Unity pooling namespace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Create the Object Pool&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a script called &lt;code&gt;BulletPool.cs&lt;/code&gt; and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Pool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BulletPool&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bulletPrefab&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;defaultCapacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxSize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;ObjectPool&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Awake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bulletPool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ObjectPool&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;CreateBullet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;OnGetBullet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;OnReleaseBullet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nc"&gt;OnDestroyBullet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;defaultCapacity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;maxSize&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="nc"&gt;CreateBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Instantiate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bulletPrefab&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnGetBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnReleaseBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetActive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;OnDestroyBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="nc"&gt;GetBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;ReturnBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Spawn and Return Bullets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, you can retrieve bullets from the pool whenever the player shoots and return them when they are no longer needed.&lt;/p&gt;

&lt;p&gt;For example, create a &lt;code&gt;PlayerShoot.cs&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerShoot&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;BulletPool&lt;/span&gt; &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Transform&lt;/span&gt; &lt;span class="n"&gt;firePoint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Shoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;bullet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;bullet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetPositionAndRotation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;firePoint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;firePoint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rotation&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a bullet hits an enemy or leaves the screen, call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;bulletPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReturnBullet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gameObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This deactivates the bullet and makes it available for reuse.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the ObjectPool Methods
&lt;/h3&gt;

&lt;p&gt;Here are the main methods and callbacks used in Unity's built-in pooling system:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method / Callback&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ObjectPool&amp;lt;T&amp;gt;()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Creates a new object pool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Get()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Retrieves an object from the pool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Release()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns an object to the pool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Clear()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Destroys objects currently stored in the pool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CountInactive&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Gets the number of inactive pooled objects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CountActive&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Gets the number of objects currently checked out&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CountAll&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Gets the total number of objects created and not destroyed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The constructor also accepts callbacks such as &lt;code&gt;actionOnGet&lt;/code&gt;, &lt;code&gt;actionOnRelease&lt;/code&gt;, and &lt;code&gt;actionOnDestroy&lt;/code&gt;, allowing you to control what happens when an object is retrieved, returned, or destroyed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom Object Pool vs Unity's Built-in Object Pool
&lt;/h3&gt;

&lt;p&gt;Both approaches help you reuse GameObjects, but there are some differences:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Custom Object Pool&lt;/th&gt;
&lt;th&gt;Unity ObjectPool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Implementation&lt;/td&gt;
&lt;td&gt;Write your own logic&lt;/td&gt;
&lt;td&gt;Built-in API&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Object creation&lt;/td&gt;
&lt;td&gt;Manual&lt;/td&gt;
&lt;td&gt;Callback-based&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Object retrieval&lt;/td&gt;
&lt;td&gt;Custom method&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Get()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Object return&lt;/td&gt;
&lt;td&gt;Custom method&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Release()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maximum size&lt;/td&gt;
&lt;td&gt;Implement manually&lt;/td&gt;
&lt;td&gt;Built-in option&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flexibility&lt;/td&gt;
&lt;td&gt;Full control&lt;/td&gt;
&lt;td&gt;Configurable callbacks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Which One Should You Use?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are learning object pooling or need a highly customized pooling system, creating your own implementation is a good way to understand how it works.&lt;/p&gt;

&lt;p&gt;However, for most Unity projects, the built-in &lt;code&gt;ObjectPool&amp;lt;T&amp;gt;&lt;/code&gt; class is a convenient choice because it provides ready-made pooling functionality with less code to maintain.&lt;/p&gt;

&lt;p&gt;Keep in mind that Unity's built-in pool is not automatically thread-safe, and its maximum size controls how many inactive objects are retained, not the maximum number of active objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Should You Use Object Pooling?
&lt;/h2&gt;

&lt;p&gt;You don't need to use pooling for every GameObject.&lt;/p&gt;

&lt;p&gt;It makes the most sense when an object is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Created frequently&lt;/li&gt;
&lt;li&gt;Destroyed frequently&lt;/li&gt;
&lt;li&gt;Reused many times&lt;/li&gt;
&lt;li&gt;Needed in large numbers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, a bullet in a shooting game is a great candidate for pooling.&lt;/p&gt;

&lt;p&gt;On the other hand, objects such as a main menu, player character, or settings panel usually don't need an object pool because they are not repeatedly created and destroyed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://learngamestutorial.com/unity-object-pooling/" rel="noopener noreferrer"&gt;Unity Object Pooling&lt;/a&gt;&lt;/strong&gt; is a simple but useful technique for games that frequently create and destroy objects.&lt;/p&gt;

&lt;p&gt;Instead of doing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Instantiate → Use → Destroy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Get From Pool → Use → Return To Pool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I used this approach in my game, it made more sense to create the objects once and reuse them rather than repeatedly creating new ones.&lt;/p&gt;

&lt;p&gt;If you're working on a shooting game, endless runner, survival game, or any project with lots of repeated objects, learning &lt;strong&gt;how to use object pool&lt;/strong&gt; can be very useful.&lt;/p&gt;

&lt;p&gt;Start with a simple pool like the one above. Once you understand the concept, you can move to Unity's built-in pooling system and create a more flexible solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. What is Unity Object Pooling?
&lt;/h3&gt;

&lt;p&gt;Unity Object Pooling is a technique that reuses GameObjects instead of repeatedly creating and destroying them.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. How do I use object pool in Unity?
&lt;/h3&gt;

&lt;p&gt;Create a group of objects in advance, get an object when you need it, activate it, and return it to the pool when you are finished with it.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. What is the Object Pooling Design Pattern?
&lt;/h3&gt;

&lt;p&gt;The Object Pooling Design Pattern is a programming pattern that stores reusable objects so they can be used again instead of being recreated.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Does object pooling improve Unity performance?
&lt;/h3&gt;

&lt;p&gt;It can improve performance when many short-lived objects are repeatedly created and destroyed. The actual benefit depends on your game's workload.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Which objects are good for object pooling?
&lt;/h3&gt;

&lt;p&gt;Bullets, enemies, projectiles, coins, explosions, particle effects, and damage numbers are common examples.&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>productivity</category>
      <category>c</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>Countdown Timer in Unity: How I Created a Timer for My Game</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Sat, 12 Sep 2026 11:48:38 +0000</pubDate>
      <link>https://dev.to/alok_krishali/countdown-timer-in-unity-how-i-created-a-timer-for-my-game-35ng</link>
      <guid>https://dev.to/alok_krishali/countdown-timer-in-unity-how-i-created-a-timer-for-my-game-35ng</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Learn Adding a Timer in unity for your Game and Scripting&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Have you ever played a game where you have only a few seconds to complete a level? That simple countdown can make the gameplay much more exciting.&lt;/p&gt;

&lt;p&gt;While working on my Unity game, I wanted to add a &lt;strong&gt;countdown timer&lt;/strong&gt; that would give the player a limited amount of time to complete a level. Instead of using a complicated system, I decided to create a simple timer using C# and Unity UI.&lt;/p&gt;

&lt;p&gt;In this tutorial, I'll show you &lt;strong&gt;how I created a countdown timer in Unity step by step&lt;/strong&gt;. We will create the timer, display it on the screen, decrease the time every second, and stop the timer when it reaches zero.&lt;/p&gt;

&lt;p&gt;By the end of this tutorial, you will have a working &lt;strong&gt;Countdown timer in Unity&lt;/strong&gt; that you can easily use in your own game.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is countdown timer? - How it works?
&lt;/h2&gt;

&lt;p&gt;The idea is simple.&lt;/p&gt;

&lt;p&gt;When the level starts, the timer will begin counting down.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;60 → 59 → 58 → 57 → ... → 1 → 0&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When the timer reaches zero, we can perform an action such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;End the level&lt;/li&gt;
&lt;li&gt;Show a game-over screen&lt;/li&gt;
&lt;li&gt;Reduce the player's score&lt;/li&gt;
&lt;li&gt;Restart the level&lt;/li&gt;
&lt;li&gt;Complete a mission&lt;/li&gt;
&lt;li&gt;Trigger another gameplay event&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For this tutorial, I'll keep the basic system simple so you can easily modify it for your own game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create a New Timer Script
&lt;/h2&gt;

&lt;p&gt;The first thing I did was create a C# script for the timer.&lt;/p&gt;

&lt;p&gt;In your Unity project, create a new C# script and name it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CountdownTimer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open the script and start with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CountdownTimer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the basic part of our &lt;strong&gt;Unity countdown timer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand what is happening here.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;timeRemaining&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This variable stores the amount of time remaining.&lt;/p&gt;

&lt;p&gt;I started the timer at &lt;strong&gt;60 seconds&lt;/strong&gt;, but you can change it to any value you want.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a 30-second timer.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Time.deltaTime&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The important part is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Time.deltaTime&lt;/code&gt; gives us the time that has passed since the previous frame.&lt;/p&gt;

&lt;p&gt;Instead of manually decreasing the timer every second, Unity continuously reduces the remaining time based on the actual game time.&lt;/p&gt;

&lt;p&gt;This makes the timer work correctly even when the game's frame rate changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Add the Timer to the Game
&lt;/h2&gt;

&lt;p&gt;Now we need to add our timer script to a GameObject.&lt;/p&gt;

&lt;p&gt;In the Unity Hierarchy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Right-click in the Hierarchy.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Create Empty&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Rename it to &lt;code&gt;GameManager&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Select the GameManager.&lt;/li&gt;
&lt;li&gt;Drag the &lt;code&gt;CountdownTimer&lt;/code&gt; script onto it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now the timer script is connected to our game.&lt;/p&gt;

&lt;p&gt;If you press Play, the &lt;code&gt;timeRemaining&lt;/code&gt; value will start decreasing.&lt;/p&gt;

&lt;p&gt;However, there is one problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The player cannot see the timer yet.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So let's add a UI element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Create the Timer UI
&lt;/h2&gt;

&lt;p&gt;For my game, I wanted the remaining time to appear at the top of the screen.&lt;/p&gt;

&lt;p&gt;First, create a Canvas if you don't already have one.&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Right-click the Canvas.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;UI → Text - TextMeshPro&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Rename it to &lt;code&gt;TimerText&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Position it where you want the timer to appear.&lt;/li&gt;
&lt;li&gt;Set the initial text to something like &lt;code&gt;60&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I recommend using TextMeshPro because it gives you better control over fonts, size, alignment, and visual appearance.&lt;/p&gt;

&lt;p&gt;Your Hierarchy might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Canvas
    └── TimerText

GameManager
    └── CountdownTimer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to connect this UI text to our C# script.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Connect the Timer UI With C
&lt;/h2&gt;

&lt;p&gt;Let's modify our script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;TMPro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CountdownTimer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;TextMeshProUGUI&lt;/span&gt; &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two important things here.&lt;/p&gt;

&lt;p&gt;First, we added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;TMPro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows us to work with TextMeshPro.&lt;/p&gt;

&lt;p&gt;Then we created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;TextMeshProUGUI&lt;/span&gt; &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This variable will hold a reference to the UI text that displays our countdown.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Assign the Timer Text in Unity
&lt;/h2&gt;

&lt;p&gt;Now go back to Unity.&lt;/p&gt;

&lt;p&gt;Select your &lt;code&gt;GameManager&lt;/code&gt; GameObject.&lt;/p&gt;

&lt;p&gt;You should see the &lt;code&gt;CountdownTimer&lt;/code&gt; component in the Inspector.&lt;/p&gt;

&lt;p&gt;There will be a field called:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Timer Text&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Simply drag your &lt;code&gt;TimerText&lt;/code&gt; GameObject from the Hierarchy into this field.&lt;/p&gt;

&lt;p&gt;Now Unity knows which UI element should display the countdown.&lt;/p&gt;

&lt;p&gt;Press &lt;strong&gt;Play&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You should see something similar to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;60
59
58
57
56
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations!&lt;/p&gt;

&lt;p&gt;You have created a basic &lt;strong&gt;countdown timer in Unity&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Why I Used &lt;code&gt;Mathf.Ceil&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;You may notice that the actual timer uses a &lt;code&gt;float&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;59.83
58.72
57.51
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We don't want to show those decimal values to the player.&lt;/p&gt;

&lt;p&gt;That's why I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This converts the remaining time into a clean whole number.&lt;/p&gt;

&lt;p&gt;So instead of displaying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;59.83
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the player sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;60
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the timer much easier to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Stop the Timer at Zero
&lt;/h2&gt;

&lt;p&gt;We don't want the timer to continue into negative numbers.&lt;/p&gt;

&lt;p&gt;That's why we use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the timer reaches zero, we simply set it to zero.&lt;/p&gt;

&lt;p&gt;Now the countdown stops at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of continuing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-1
-2
-3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 8: What Happens When the Timer Reaches Zero?
&lt;/h2&gt;

&lt;p&gt;This is where you can customize the timer for your own game.&lt;/p&gt;

&lt;p&gt;For example, in my game, I could trigger a game-over function when the timer reaches zero.&lt;/p&gt;

&lt;p&gt;We can modify the code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;TMPro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CountdownTimer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;TextMeshProUGUI&lt;/span&gt; &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;timerFinished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;timerFinished&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;timerFinished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nc"&gt;TimerFinished&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;TimerFinished&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Time's Up!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;TimerFinished()&lt;/code&gt; is called when the countdown reaches zero.&lt;/p&gt;

&lt;p&gt;You could replace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Time's Up!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with your actual game logic.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;GameOver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;CompleteMission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;RestartLevel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the timer useful for actual gameplay rather than just displaying numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Add Minutes and Seconds
&lt;/h2&gt;

&lt;p&gt;If your game has a longer timer, displaying only seconds may not look very good.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;125
124
123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, you may want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;02:05
02:04
02:03
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can easily change the display.&lt;/p&gt;

&lt;p&gt;Replace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FloorToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FloorToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"{0:00}:{1:00}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a 125-second timer will display:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;02:05
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;02:04
02:03
02:02
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a much better format for many games.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 10: Add a Visual Warning
&lt;/h2&gt;

&lt;p&gt;A countdown timer becomes even more useful when it gives the player a warning that time is running out.&lt;/p&gt;

&lt;p&gt;For example, when there are only 10 seconds remaining, we can change the timer's appearance.&lt;/p&gt;

&lt;p&gt;A simple approach is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the timer can become red when the player is running out of time.&lt;/p&gt;

&lt;p&gt;You can take this further by adding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Timer animation&lt;/li&gt;
&lt;li&gt;Sound effects&lt;/li&gt;
&lt;li&gt;Screen effects&lt;/li&gt;
&lt;li&gt;Countdown sounds&lt;/li&gt;
&lt;li&gt;Camera shake&lt;/li&gt;
&lt;li&gt;Warning messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TIME: 10
TIME: 9
TIME: 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final few seconds can create a lot more tension during gameplay.&lt;/p&gt;

&lt;h2&gt;
  
  
  Complete Countdown Timer Script for unity Game
&lt;/h2&gt;

&lt;p&gt;After putting everything together, this is the basic version I would use for a simple game timer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nc"&gt;TMPro&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CountdownTimer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;60f&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;TextMeshProUGUI&lt;/span&gt; &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;timerFinished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nc"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deltaTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;timerFinished&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;timerFinished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="nc"&gt;TimerFinished&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FloorToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FloorToInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"{0:00}:{1:00}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;timerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nc"&gt;TimerFinished&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Time's Up!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now connect this script to your UI and use it as the foundation for a timer system in your Unity game.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I Used the Timer in My Game
&lt;/h2&gt;

&lt;p&gt;The main reason I created this system was to add a little more pressure to the gameplay.&lt;/p&gt;

&lt;p&gt;Without a timer, a player can sometimes take as much time as they want to complete a level. That's fine for some games, but for challenge-based gameplay, a countdown can make each level feel more engaging.&lt;/p&gt;

&lt;p&gt;The basic gameplay flow becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start Level
     ↓
Start Countdown
     ↓
Player Plays
     ↓
Timer Decreases
     ↓
Player Completes Level?
     ↓
Yes → Level Complete
     ↓
No → Timer Reaches 0
     ↓
Time's Up / Game Over
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple system can be adapted to many different types of games.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where You Can Use a Timer in Unity
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;timer in Unity&lt;/strong&gt; can be used in many gameplay situations.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;h3&gt;
  
  
  Puzzle Games
&lt;/h3&gt;

&lt;p&gt;Give the player 60 seconds to solve a puzzle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Action Games
&lt;/h3&gt;

&lt;p&gt;Give the player a limited amount of time to defeat enemies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Racing Games
&lt;/h3&gt;

&lt;p&gt;Use a countdown to control race events or time trials.&lt;/p&gt;

&lt;h3&gt;
  
  
  Survival Games
&lt;/h3&gt;

&lt;p&gt;Challenge the player to survive for a specific amount of time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Platform Games
&lt;/h3&gt;

&lt;p&gt;Give the player a limited amount of time to reach the finish point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mission-Based Games
&lt;/h3&gt;

&lt;p&gt;Create missions where the player needs to complete an objective before the timer reaches zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Problems When Creating a Timer in Unity
&lt;/h2&gt;

&lt;p&gt;While creating a countdown timer is relatively simple, there are a few common problems you may encounter.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timer Goes Below Zero
&lt;/h3&gt;

&lt;p&gt;Make sure you set:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;when the countdown finishes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timer Shows Decimal Numbers
&lt;/h3&gt;

&lt;p&gt;If you see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;59.82342
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="nc"&gt;Mathf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Ceil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeRemaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or format the timer as minutes and seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timer UI Is Not Updating
&lt;/h3&gt;

&lt;p&gt;Check that you have dragged the &lt;code&gt;TimerText&lt;/code&gt; object into the &lt;code&gt;Timer Text&lt;/code&gt; field in the Inspector.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timer Finishes Multiple Times
&lt;/h3&gt;

&lt;p&gt;Use a boolean such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;timerFinished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This prevents your end-of-timer function from being called repeatedly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Creating a &lt;strong&gt;&lt;a href="https://learngamestutorial.com/countdown-timer-in-unity/" rel="noopener noreferrer"&gt;countdown timer in Unity&lt;/a&gt;&lt;/strong&gt; doesn't have to be complicated. I started with a simple &lt;code&gt;float&lt;/code&gt; variable, reduced it using &lt;code&gt;Time.deltaTime&lt;/code&gt;, and then connected it to a TextMeshPro UI element.&lt;/p&gt;

&lt;p&gt;From there, it is easy to expand the system with game-over logic, sounds, animations, warnings, and other gameplay features.&lt;/p&gt;

&lt;p&gt;The nice thing about this approach is that you don't need a complicated timer framework. You can start with this simple system and build on it as your game becomes more advanced.&lt;/p&gt;

&lt;p&gt;If you're learning Unity or working on your own game, I recommend starting with the simple version first. Once it works, add features one at a time.&lt;/p&gt;

&lt;p&gt;That's how I created a simple &lt;strong&gt;countdown timer for my Unity game&lt;/strong&gt;, and you can use the same approach in your own project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. How do I create a countdown timer in Unity?
&lt;/h3&gt;

&lt;p&gt;You can create a countdown timer in Unity using a &lt;code&gt;float&lt;/code&gt; variable and decrease it with &lt;code&gt;Time.deltaTime&lt;/code&gt;. You can then display the remaining time using a TextMeshPro UI element.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. How do I create a timer in Unity using C#?
&lt;/h3&gt;

&lt;p&gt;Create a C# script with a &lt;code&gt;float&lt;/code&gt; variable for the time and decrease it inside the &lt;code&gt;Update()&lt;/code&gt; method using &lt;code&gt;Time.deltaTime&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. How do I display a countdown timer in Unity?
&lt;/h3&gt;

&lt;p&gt;Create a TextMeshPro UI text object and connect it to your timer script. Update the text using the remaining time.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. How do I stop a timer when it reaches zero in Unity?
&lt;/h3&gt;

&lt;p&gt;Check whether the remaining time is greater than zero. If it reaches zero, set the value to zero and run your desired function.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Can I use a countdown timer for a Unity game level?
&lt;/h3&gt;

&lt;p&gt;Yes. A &lt;a href="https://learngamestutorial.com/countdown-timer-in-unity/" rel="noopener noreferrer"&gt;countdown timer&lt;/a&gt; can be used for timed levels, missions, puzzles, races, survival challenges, and many other gameplay situations.&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>programming</category>
      <category>csharp</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Unity WebGL + React: How I Made C# and TypeScript Talk to Each Other</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Tue, 01 Sep 2026 05:02:26 +0000</pubDate>
      <link>https://dev.to/alok_krishali/unity-webgl-react-how-i-made-c-and-typescript-talk-to-each-other-2plg</link>
      <guid>https://dev.to/alok_krishali/unity-webgl-react-how-i-made-c-and-typescript-talk-to-each-other-2plg</guid>
      <description>&lt;p&gt;I’ve seen many gaming websites where games run directly in the browser, and the experience can be surprisingly smooth. Since the web has a huge audience, I realized that many users would prefer to play a game instantly without downloading or installing anything.&lt;/p&gt;

&lt;p&gt;That got me thinking: &lt;strong&gt;why not bring my Unity games to the web using Unity WebGL?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I already knew how to build games in Unity, but when I wanted to integrate a Unity WebGL application into a modern React application, I ran into a different challenge:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How do two completely different environments—Unity/C# and React/TypeScript—communicate with each other?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At first, I thought this would be a simple task.&lt;/p&gt;

&lt;p&gt;It wasn't.&lt;/p&gt;

&lt;p&gt;After trying different approaches, testing the communication, and fixing a few issues along the way, I finally got a working setup.&lt;/p&gt;

&lt;p&gt;In this article, I’ll share the approach I used to connect &lt;strong&gt;Unity WebGL + React + TypeScript&lt;/strong&gt;, including how I send data in both directions.&lt;/p&gt;




&lt;h2&gt;
  
  
  What We Are Building
&lt;/h2&gt;

&lt;p&gt;Before writing any code, let's understand what we want to achieve.&lt;/p&gt;

&lt;p&gt;My basic setup looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React + TypeScript
       ↓
react-unity-webgl
       ↓
Unity WebGL
       ↓
Unity C# Scripts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal is simple.&lt;/p&gt;

&lt;p&gt;I want React to be able to tell &lt;a href="https://unity.com/" rel="noopener noreferrer"&gt;Unity &lt;/a&gt;something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Start Level 5"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I also want Unity to send information back to React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Level Completed"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we need &lt;strong&gt;two-way communication&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React  →  Unity
React  ←  Unity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once I understood this, the implementation became much easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Create the Unity Project
&lt;/h2&gt;

&lt;p&gt;I started with a normal Unity project.&lt;/p&gt;

&lt;p&gt;For this example, let's assume I have a simple game with a &lt;code&gt;GameManager&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;The important part is that the GameObject we want React to communicate with must have a C# script attached to it.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GameManager&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SetPlayerName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;playerName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Player Name: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;playerName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I created a public method called &lt;code&gt;SetPlayerName&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This method will later be called from React.&lt;/p&gt;

&lt;p&gt;The important thing to remember is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React doesn't directly call a C# method.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/pulse/explore-steps-involve-react-unity-webgl-communication-alok-krishali-hvrze/?trackingId=13C2WZOZTvGUsYFlzBWlkA%3D%3D" rel="noopener noreferrer"&gt;React communicates with the Unity WebGL&lt;/a&gt; instance, and Unity then calls the method on the specified GameObject.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Build Unity for WebGL
&lt;/h2&gt;

&lt;p&gt;Next, I changed the Unity build platform to WebGL.&lt;/p&gt;

&lt;p&gt;In Unity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File
 → Build Settings
 → WebGL
 → Switch Platform
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I created a WebGL build.&lt;/p&gt;

&lt;p&gt;The output contains files required to run the Unity game in a browser.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebGL_Build/
├── Build/
├── TemplateData/
└── index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I don't recommend manually modifying the generated Unity files unless you know exactly what you're changing.&lt;/p&gt;

&lt;p&gt;Let Unity generate the build and let React handle displaying it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: Create the React Application
&lt;/h2&gt;

&lt;p&gt;For the frontend, I used React with TypeScript.&lt;/p&gt;

&lt;p&gt;I created a Vite application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm create vite@latest react-unity-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then selected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React
TypeScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the project, I installed the package that handles &lt;a href="https://docs.unity3d.com/560/Documentation/Manual/webgl-gettingstarted.html" rel="noopener noreferrer"&gt;Unity WebGL&lt;/a&gt; integration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;react-unity-webgl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This package makes it much easier to load and communicate with a Unity WebGL application from React.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Load Unity Inside React
&lt;/h2&gt;

&lt;p&gt;Now we need to connect our &lt;a href="https://learngamestutorial.com/unity-webgl-games/" rel="noopener noreferrer"&gt;Unity WebGL build&lt;/a&gt; with React.&lt;/p&gt;

&lt;p&gt;I created a Unity component.&lt;/p&gt;

&lt;p&gt;A simplified version looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Unity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useUnityContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-unity-webgl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;unityProvider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useUnityContext&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;loaderUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/WebGL_Build/Build/WebGL_Build.loader.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;dataUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/WebGL_Build/Build/WebGL_Build.data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;frameworkUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/WebGL_Build/Build/WebGL_Build.framework.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;codeUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/WebGL_Build/Build/WebGL_Build.wasm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Unity&lt;/span&gt;
            &lt;span class="na"&gt;unityProvider&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;unityProvider&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
            &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100%&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;600px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact file names will depend on your Unity build.&lt;/p&gt;

&lt;p&gt;This was one of the first places where I faced problems.&lt;/p&gt;

&lt;p&gt;If the paths are incorrect, React may load successfully but Unity won't.&lt;/p&gt;

&lt;p&gt;You might see errors such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;404 Not Found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So always check the browser's developer console and Network tab when the Unity build doesn't load.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: The Interesting Part — React to Unity
&lt;/h2&gt;

&lt;p&gt;Now let's make React communicate with Unity.&lt;/p&gt;

&lt;p&gt;This is where things started getting interesting for me.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;react-unity-webgl&lt;/code&gt; package provides a way to send messages to Unity.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;unityProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sendMessage&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useUnityContext&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;loaderUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;dataUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;frameworkUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;codeUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;...&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can create a button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
    &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GameManager&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;SetPlayerName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alok&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    Send Name to Unity
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's understand what is happening here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"GameManager"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is the name of the GameObject in Unity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"SetPlayerName"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is the C# method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Alok"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is the value we're sending.&lt;/p&gt;

&lt;p&gt;So React is effectively saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Unity, find the GameObject called &lt;code&gt;GameManager&lt;/code&gt; and call &lt;code&gt;SetPlayerName()&lt;/code&gt; with this value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unity receives it through the WebGL communication layer and executes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;SetPlayerName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;playerName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Debug&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Player Name: "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;playerName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's our first successful communication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeScript
    ↓
Unity WebGL
    ↓
C#
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 6: Sending Numbers and Other Data
&lt;/h2&gt;

&lt;p&gt;Strings are easy.&lt;/p&gt;

&lt;p&gt;But real applications usually need more than strings.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Player ID
Level Number
Score
Game Settings
User Information
Mission Data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For simple values, you can pass numbers or strings.&lt;/p&gt;

&lt;p&gt;For more complex data, I prefer using JSON.&lt;/p&gt;

&lt;p&gt;For example, React can send:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"playerName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alok"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"level"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"score"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the Unity side, I can create a C# class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PlayerData&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;playerName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then deserialize the JSON inside Unity.&lt;/p&gt;

&lt;p&gt;This approach becomes especially useful when the React application is acting as the main web interface and Unity is responsible mainly for the 3D/game experience.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 7: Unity to React
&lt;/h2&gt;

&lt;p&gt;Now comes the other direction.&lt;/p&gt;

&lt;p&gt;We have already done:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React → Unity
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in a real application, Unity also needs to send information back.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Level Started
Level Completed
Player Died
Score Changed
Mission Completed
Game Loaded
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where a JavaScript bridge becomes useful.&lt;/p&gt;

&lt;p&gt;The basic idea is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unity C#
   ↓
JavaScript
   ↓
React
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unity can call a JavaScript function exposed by the WebGL page.&lt;/p&gt;

&lt;p&gt;For example, conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;LevelCompleted&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Notify the web application&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JavaScript side can then forward that event to React.&lt;/p&gt;

&lt;p&gt;In React, I can listen for the event and update the UI.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unity
  ↓
"LevelCompleted"
  ↓
React
  ↓
Show "Congratulations!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This separation is useful because Unity doesn't need to know how the React UI works.&lt;/p&gt;

&lt;p&gt;Unity only needs to say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The level has been completed."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React decides what to do with that information.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 8: A Real Example
&lt;/h2&gt;

&lt;p&gt;This is where I found the architecture much more useful.&lt;/p&gt;

&lt;p&gt;Imagine we're building an online game portal.&lt;/p&gt;

&lt;p&gt;The React application contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Header
Login
Profile
Game List
Leaderboard
Game Screen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unity handles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3D Game
Player
Gameplay
Physics
Animations
Game Logic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine the user selects a game from React.&lt;/p&gt;

&lt;p&gt;React tells Unity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load Level 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unity loads the level.&lt;/p&gt;

&lt;p&gt;After the player finishes it, Unity sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Level Completed
Score: 2500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React receives the event and can:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Update the result screen
Save the score
Show leaderboard
Move to the next level
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the architecture becomes much clearer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             React
        ┌──────────────┐
        │ UI           │
        │ Login        │
        │ Profile      │
        │ Leaderboard  │
        └──────┬───────┘
               │
         Communication
               │
        ┌──────▼───────┐
        │ Unity WebGL  │
        │              │
        │ Gameplay     │
        │ 3D          │
        │ Physics      │
        └──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we add a backend later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React
  ↓
.NET Web API
  ↓
Database

React
  ↕
Unity WebGL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the architecture I am currently more interested in because it allows Unity to become part of a larger web application instead of being the entire website.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 9: Problems I Faced
&lt;/h2&gt;

&lt;p&gt;The communication itself wasn't the only challenge.&lt;/p&gt;

&lt;p&gt;I also faced a few common issues while putting everything together.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Unity WebGL files returning 404
&lt;/h3&gt;

&lt;p&gt;Usually this was related to incorrect build paths.&lt;/p&gt;

&lt;p&gt;I checked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;loaderUrl
dataUrl
frameworkUrl
codeUrl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and made sure the generated files were actually available to React.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Unity loading but communication not working
&lt;/h3&gt;

&lt;p&gt;In this case, I checked three things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GameObject name
       ↓
C# method name
       ↓
Parameter type
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even a small mismatch can cause problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. React errors
&lt;/h3&gt;

&lt;p&gt;I also ran into React-side issues such as incorrect hook usage and import/path problems.&lt;/p&gt;

&lt;p&gt;My biggest lesson was simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't debug Unity and React at the same time.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First confirm that Unity WebGL works by itself.&lt;/p&gt;

&lt;p&gt;Then confirm React can load Unity.&lt;/p&gt;

&lt;p&gt;Finally, test communication.&lt;/p&gt;

&lt;p&gt;Breaking the problem into these smaller steps saved me a lot of time.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;The biggest lesson from this project was that &lt;strong&gt;Unity WebGL doesn't have to live alone&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unity is very good at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;3D&lt;/li&gt;
&lt;li&gt;gameplay&lt;/li&gt;
&lt;li&gt;physics&lt;/li&gt;
&lt;li&gt;animations&lt;/li&gt;
&lt;li&gt;game systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React is very good at:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;web UI&lt;/li&gt;
&lt;li&gt;authentication&lt;/li&gt;
&lt;li&gt;dashboards&lt;/li&gt;
&lt;li&gt;navigation&lt;/li&gt;
&lt;li&gt;forms&lt;/li&gt;
&lt;li&gt;application state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of trying to make one technology do everything, we can let each technology do what it is good at.&lt;/p&gt;

&lt;p&gt;The communication layer becomes the bridge between them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;React / TypeScript
       ↕
 Communication Layer
       ↕
Unity WebGL / C#
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And once this basic communication is working, there are many possibilities—online games, training applications, digital twins, interactive 3D websites, simulations, and much more.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;When I started this project, my main question was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How can React and Unity communicate with each other?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After working through it, I realized that the difficult part isn't actually sending a message.&lt;/p&gt;

&lt;p&gt;The more important part is &lt;strong&gt;designing the boundary between the two applications&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;React doesn't need to know how Unity moves a character.&lt;/p&gt;

&lt;p&gt;Unity doesn't need to know how React renders a dashboard.&lt;/p&gt;

&lt;p&gt;They just need a clean way to exchange the information they actually need.&lt;/p&gt;

&lt;p&gt;That was the approach that finally made the whole setup click for me.&lt;/p&gt;

&lt;p&gt;If you're also working with &lt;strong&gt;Unity WebGL + React&lt;/strong&gt;, I'd genuinely like to know how you're handling communication.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Are you using &lt;code&gt;[react-unity-webgl](https://www.npmjs.com/package/react-unity-webgl)&lt;/code&gt;, your own JavaScript bridge, or a different approach?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found a better way, share it in the comments—I’d love to learn from it. And if you ran into an issue while implementing this, &lt;strong&gt;leave a comment below&lt;/strong&gt;. I’ll be happy to discuss it.&lt;/p&gt;

&lt;p&gt;Thanks for reading! 🚀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Suggested by Writer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learngamestutorial.com/how-to-create-your-first-unity-game/" rel="noopener noreferrer"&gt;How to Create Your First Unity Game in 24 Hours&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learngamestutorial.com/unity-webgl-optimization/" rel="noopener noreferrer"&gt;Unity WebGL Optimization for Lightning-Fast Performance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learngamestutorial.com/creating-stunning-unity-webgl-games/" rel="noopener noreferrer"&gt;Creating Stunning Unity WebGL Games: A Step-by-Step Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>unity3d</category>
      <category>webgl</category>
      <category>react</category>
      <category>webapp</category>
    </item>
    <item>
      <title>Is UI Overdraw Killing Your WebGL Performance?</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Mon, 17 Aug 2026 06:12:05 +0000</pubDate>
      <link>https://dev.to/alok_krishali/is-ui-overdraw-killing-your-webgl-performance-2o92</link>
      <guid>https://dev.to/alok_krishali/is-ui-overdraw-killing-your-webgl-performance-2o92</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why Is Overdraw Particularly Important in WebGL?&lt;/strong&gt;&lt;br&gt;
When optimizing a Unity WebGL application, developers often focus on polygon count, draw calls, texture size, and shader complexity. But there is another performance problem that can quietly become expensive: &lt;strong&gt;UI overdraw&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A UI may look simple to the player while the GPU is actually rendering the same pixels several times.&lt;/p&gt;

&lt;p&gt;A typical game interface might contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A full-screen background&lt;/li&gt;
&lt;li&gt;Transparent panels&lt;/li&gt;
&lt;li&gt;Multiple decorative images&lt;/li&gt;
&lt;li&gt;Buttons&lt;/li&gt;
&lt;li&gt;Shadows&lt;/li&gt;
&lt;li&gt;Icons&lt;/li&gt;
&lt;li&gt;Text&lt;/li&gt;
&lt;li&gt;Semi-transparent overlays&lt;/li&gt;
&lt;li&gt;Popups&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Individually, these elements may seem harmless. When stacked together, however, they can significantly increase the number of pixels the GPU needs to process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.unity3d.com/cn/2021.3/Manual/OptimizingGraphicsPerformance.html" rel="noopener noreferrer"&gt;Unity &lt;/a&gt;identifies overlapping transparent UI, sprites, and particles as common contributors to overdraw.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;is UI overdraw really killing your WebGL performance?&lt;/strong&gt; Let's understand when it matters and how to reduce it.&lt;/p&gt;


&lt;h2&gt;
  
  
  What Exactly Is UI Overdraw?
&lt;/h2&gt;

&lt;p&gt;Overdraw occurs when the same screen pixel is rendered multiple times during a frame.&lt;/p&gt;

&lt;p&gt;Imagine a 1920×1080 &lt;a href="https://learngamestutorial.com/unity-webgl-games/" rel="noopener noreferrer"&gt;WebGL game&lt;/a&gt; with this UI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Full-screen background
        ↓
Transparent dark overlay
        ↓
Semi-transparent panel
        ↓
Panel decoration
        ↓
Button background
        ↓
Button icon
        ↓
Button text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A pixel covered by all these elements may be processed repeatedly.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             GPU
              ↓
       ┌──────────────┐
       │ Background   │  ← 1
       │ Overlay      │  ← 2
       │ Panel        │  ← 3
       │ Decoration   │  ← 4
       │ Button       │  ← 5
       │ Icon         │  ← 6
       │ Text         │  ← 7
       └──────────────┘

          Same pixel
          processed
          multiple times
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is particularly relevant when your UI contains large transparent elements covering substantial portions of the screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.unity3d.com/cn/2021.3/Manual/OptimizingGraphicsPerformance.html" rel="noopener noreferrer"&gt;Unity's graphics documentation&lt;/a&gt; specifically recommends identifying and reducing overdraw when fill rate becomes a GPU limitation.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Is Overdraw Particularly Important in WebGL?
&lt;/h1&gt;

&lt;p&gt;WebGL runs inside a browser, so performance has additional constraints compared with a native desktop application.&lt;/p&gt;

&lt;p&gt;If your application is GPU-bound, rendering unnecessary pixels means the GPU has more work to complete every frame.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Low Overdraw

Background
████████████████

     Button
      ████

GPU workload → relatively low
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;versus:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;High Overdraw

████████████████
████████████████
████████████████
████████████████
████████████████

Several transparent layers

GPU workload → much higher
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important point is that &lt;strong&gt;screen resolution matters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A small transparent icon might have negligible impact.&lt;/p&gt;

&lt;p&gt;A transparent image covering the entire 1920×1080 screen is a completely different story.&lt;/p&gt;




&lt;h1&gt;
  
  
  How Can You Identify UI Overdraw in Unity?
&lt;/h1&gt;

&lt;p&gt;Don't optimize blindly.&lt;/p&gt;

&lt;p&gt;Unity provides an &lt;strong&gt;Overdraw visualization&lt;/strong&gt; that lets you see areas where multiple objects are being rendered over each other. The UI Profiler can also help analyze UI batching and identify which parts of the interface are contributing to &lt;a href="https://docs.unity3d.com/jp/2019.1/Manual/ProfilerUI.html" rel="noopener noreferrer"&gt;performance problems&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the Unity Editor, inspect your scene using the appropriate rendering/debug visualization and look for areas with heavy overlapping UI.&lt;/p&gt;

&lt;p&gt;You might discover something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Main Menu

Background
   +
Gradient
   +
Clouds
   +
Glow
   +
Decorative frame
   +
Transparent panel
   +
Buttons
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visually it looks beautiful.&lt;/p&gt;

&lt;p&gt;From the GPU's perspective, however, large portions of the screen may be getting processed several times.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't optimize based only on how the UI looks.
&lt;/h3&gt;

&lt;p&gt;Profile it.&lt;/p&gt;

&lt;p&gt;Unity's Profiler is designed to help identify CPU, GPU, rendering, memory and other performance bottlenecks, and Unity recommends profiling on the target platform when &lt;a href="https://docs.unity3d.com/es/2021.1/Manual/ProfilerWindow.html" rel="noopener noreferrer"&gt;evaluating optimizations&lt;/a&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Which UI Elements Usually Cause High Overdraw?
&lt;/h1&gt;

&lt;p&gt;Some UI patterns deserve special attention.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Full-screen transparent images
&lt;/h3&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Canvas
 ├── Background
 ├── Transparent Overlay
 ├── Gradient
 └── Vignette
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all four cover the entire screen, you're processing millions of pixels multiple times.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Large semi-transparent panels
&lt;/h3&gt;

&lt;p&gt;A panel that occupies 80% of the screen can be considerably more expensive than a small popup.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Shadows and outlines
&lt;/h3&gt;

&lt;p&gt;Visual effects can add additional rendering work, particularly when they're applied to many elements.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100 buttons
+
100 shadows
+
100 outlines
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;consider whether the visual effect can be incorporated into the sprite itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Decorative UI
&lt;/h3&gt;

&lt;p&gt;Things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;glows&lt;/li&gt;
&lt;li&gt;highlights&lt;/li&gt;
&lt;li&gt;gradients&lt;/li&gt;
&lt;li&gt;particles&lt;/li&gt;
&lt;li&gt;transparent borders&lt;/li&gt;
&lt;li&gt;shine effects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;can accumulate quickly.&lt;/p&gt;

&lt;p&gt;The problem isn't necessarily one element.&lt;/p&gt;

&lt;p&gt;It's the &lt;strong&gt;stacking of many elements&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  How Can You Reduce UI Overdraw?
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. Remove unnecessary transparent layers
&lt;/h2&gt;

&lt;p&gt;Suppose you have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Panel
 ├── Background
 ├── Gradient
 ├── Glow
 └── Highlight
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ask whether all four are necessary.&lt;/p&gt;

&lt;p&gt;Sometimes you can combine them into a single optimized sprite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Panel
 └── Optimized Sprite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Can I make this texture smaller?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;also ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"How many times am I rendering this pixel?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's often the more important question for overdraw.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Bake static effects into sprites
&lt;/h2&gt;

&lt;p&gt;Suppose you have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Panel
 ├── Base Image
 ├── Shadow
 ├── Glow
 └── Border
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If these never change independently, you might be able to combine them into one texture.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BEFORE

Base
 +
Shadow
 +
Glow
 +
Border


AFTER

One optimized sprite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can reduce the number of overlapping transparent layers.&lt;/p&gt;

&lt;p&gt;The trade-off is flexibility: if you need to animate the glow or change the border independently, keeping them separate may be preferable.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. Don't render invisible UI
&lt;/h1&gt;

&lt;p&gt;One surprisingly common mistake is making UI visually invisible without actually disabling it.&lt;/p&gt;

&lt;p&gt;For example, in UI Toolkit, Unity notes that setting opacity to zero does not necessarily eliminate the rendering cost, whereas hiding an element completely can avoid that rendering.&lt;/p&gt;

&lt;p&gt;The same principle is useful when designing Unity UI systems generally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Popup hidden

Bad:
Popup remains active
Alpha = 0

Better:
Popup is actually inactive/removed when appropriate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For frequently used UI, you can keep objects pooled and inactive rather than continually rendering transparent objects.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Be careful with full-screen overlays
&lt;/h1&gt;

&lt;p&gt;This is especially important for games.&lt;/p&gt;

&lt;p&gt;Imagine your pause menu:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Gameplay
   ↓
Black transparent overlay
   ↓
Pause panel
   ↓
Buttons
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The overlay covers the entire screen.&lt;/p&gt;

&lt;p&gt;If your &lt;a href="https://play.google.com/store/apps/details?id=com.AlokKrishali.SeaPop" rel="noopener noreferrer"&gt;gameplay &lt;/a&gt;is still rendered underneath it, you're effectively doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3D scene
+
full-screen transparent layer
+
pause UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sometimes that's necessary.&lt;/p&gt;

&lt;p&gt;But if the underlying scene doesn't need to remain visible, consider whether you can stop or simplify what is being rendered underneath.&lt;/p&gt;

&lt;p&gt;This is one reason a pause/menu architecture should be designed intentionally rather than simply placing another giant Canvas over the game.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Optimize UI architecture, not just textures
&lt;/h1&gt;

&lt;p&gt;Overdraw isn't the only &lt;a href="https://docs.unity3d.com/ja/current/Manual/best-practice-guides/ui-toolkit-for-advanced-unity-developers/optimizing-performance.html" rel="noopener noreferrer"&gt;UI performance issue&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Unity's UI Profiler can expose layout and batching behavior, while Unity's documentation also highlights issues such as layout recalculation, vertex-buffer updates, masking, and rendering state changes as potential sources of UI cost.&lt;/p&gt;

&lt;p&gt;For example, this can become problematic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Canvas
 └── VerticalLayoutGroup
      └── ContentSizeFitter
           └── Many children
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;especially when the hierarchy is constantly changing.&lt;/p&gt;

&lt;p&gt;A better approach for dynamic interfaces can be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Static UI
     ↓
Minimal rebuilding

Dynamic UI
     ↓
Separate Canvas

Large Lists
     ↓
Virtualization / pooling
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where UI optimization becomes much more than simply reducing texture sizes.&lt;/p&gt;




&lt;h1&gt;
  
  
  What About Texture Atlases?
&lt;/h1&gt;

&lt;p&gt;Texture atlasing can help, but it solves a &lt;strong&gt;different problem&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Suppose you have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Button.png
Coin.png
Heart.png
Star.png
Arrow.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can combine them into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UI_Atlas.png
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and use appropriate sprite regions.&lt;/p&gt;

&lt;p&gt;This can improve batching by reducing texture changes when the UI elements can be batched together. Unity's documentation notes that grouping textures into an atlas can &lt;a href="https://docs.unity3d.com/cn/2022.3/Manual/UIE-performance-consideration-runtime.html" rel="noopener noreferrer"&gt;reduce batches caused by texture changes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But remember:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Texture atlas ≠ automatic overdraw reduction.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 texture atlas
+
1 material
+
10 transparent layers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and still have high overdraw.&lt;/p&gt;

&lt;p&gt;So think about two separate problems:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Texture Atlas
      ↓
Helps batching / texture changes

Overdraw optimization
      ↓
Reduces unnecessary pixel processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both are important, but they address different bottlenecks.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Practical WebGL UI Optimization Strategy
&lt;/h1&gt;

&lt;p&gt;For a production Unity WebGL project, I would approach UI optimization in this order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Profile
      ↓
2. Check GPU vs CPU bottleneck
      ↓
3. Inspect UI overdraw
      ↓
4. Remove unnecessary transparent layers
      ↓
5. Combine static visual effects
      ↓
6. Split dynamic and static UI
      ↓
7. Optimize Canvas rebuilds
      ↓
8. Use Sprite Atlases
      ↓
9. Reduce unnecessary masks/effects
      ↓
10. Test on real target hardware/browser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't assume that reducing draw calls will solve every problem.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Draw Calls: LOW
Overdraw: HIGH
GPU: HIGH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your application can still perform poorly.&lt;/p&gt;

&lt;p&gt;Conversely:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Draw Calls: HIGH
Overdraw: LOW
GPU: LOW
CPU: HIGH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your bottleneck may be somewhere completely different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Measure first, then optimize the actual bottleneck.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;UI &lt;a href="https://learngamestutorial.com/unity-webgl-optimization/" rel="noopener noreferrer"&gt;optimization in Unity WebGL&lt;/a&gt; isn't about making your interface look simple. It's about making the rendering pipeline &lt;strong&gt;do only the work that is actually necessary&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Overdraw becomes particularly dangerous when large transparent elements overlap:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Large UI
   +
Transparency
   +
Effects
   +
Multiple layers
   =
High Pixel Processing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The most effective approach is to combine several techniques:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce unnecessary transparent layers&lt;/li&gt;
&lt;li&gt;Bake static visual effects into sprites&lt;/li&gt;
&lt;li&gt;Avoid unnecessary full-screen overlays&lt;/li&gt;
&lt;li&gt;Disable UI that isn't being displayed&lt;/li&gt;
&lt;li&gt;Use Sprite Atlases where appropriate&lt;/li&gt;
&lt;li&gt;Separate static and frequently changing UI&lt;/li&gt;
&lt;li&gt;Reduce expensive masking/effects&lt;/li&gt;
&lt;li&gt;Profile overdraw and GPU usage&lt;/li&gt;
&lt;li&gt;Test the final WebGL build rather than relying only on the Editor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And perhaps the most important rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Don't optimize UI based on the number of elements alone. Optimize based on how much work those elements make the CPU and GPU perform.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For WebGL, that distinction can make the difference between a UI that merely works in the browser and one that feels genuinely responsive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Profile → identify → optimize → measure again.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That should be the foundation of your &lt;a href="https://docs.unity3d.com/es/2021.1/Manual/ProfilerWindow.html" rel="noopener noreferrer"&gt;Unity WebGL UI optimization&lt;/a&gt; workflow.&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>webgl</category>
      <category>unityoptimization</category>
      <category>unityui</category>
    </item>
    <item>
      <title>Free AI Tools Every Unity Developer Should Use to Build Games Faster</title>
      <dc:creator>Alok Krishali</dc:creator>
      <pubDate>Mon, 01 Jun 2026 05:56:05 +0000</pubDate>
      <link>https://dev.to/alok_krishali/free-ai-tools-every-unity-developer-should-use-to-build-games-faster-1bo4</link>
      <guid>https://dev.to/alok_krishali/free-ai-tools-every-unity-developer-should-use-to-build-games-faster-1bo4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Game development has never been more accessible, but creating a successful game still requires significant time and effort. Unity developers often spend countless hours writing scripts, creating assets, debugging code, and testing gameplay mechanics. Fortunately, modern AI tools can automate many of these repetitive tasks, helping developers focus on creativity and game design.&lt;/p&gt;

&lt;p&gt;The best part? Many powerful AI tools offer free plans, open-source versions, or community editions that can dramatically improve productivity without increasing development costs.&lt;/p&gt;

&lt;p&gt;In this guide, we'll explore five free AI tools that every Unity developer should consider using to build games faster and more efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. ChatGPT: Your AI Programming Assistant for Unity
&lt;/h2&gt;

&lt;p&gt;One of the most versatile AI tools available today is ChatGPT. Unity developers can use it as a coding assistant, debugging partner, and learning resource.&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Helps
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Generate C# scripts for player movement, inventory systems, and enemy behaviors.&lt;/li&gt;
&lt;li&gt;Explain Unity errors and compiler messages.&lt;/li&gt;
&lt;li&gt;Optimize existing code for better performance.&lt;/li&gt;
&lt;li&gt;Create game design documents and development roadmaps.&lt;/li&gt;
&lt;li&gt;Generate dialogue, quests, and story ideas.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Practical Example
&lt;/h3&gt;

&lt;p&gt;Instead of spending 30 minutes writing a basic health system, you can ask ChatGPT to generate a complete Unity-compatible script and then customize it according to your project requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Use Case
&lt;/h3&gt;

&lt;p&gt;Perfect for indie developers, students, and small teams that need quick coding assistance without hiring additional programmers.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. GitHub Copilot: AI Pair Programming Inside Your Editor
&lt;/h2&gt;

&lt;p&gt;GitHub Copilot has become one of the most widely adopted AI coding assistants for developers. It works directly inside Visual Studio and Visual Studio Code, making it highly useful for Unity projects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Benefits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Real-time code completion.&lt;/li&gt;
&lt;li&gt;Automatic function generation.&lt;/li&gt;
&lt;li&gt;Faster bug fixing.&lt;/li&gt;
&lt;li&gt;Intelligent suggestions based on project context.&lt;/li&gt;
&lt;li&gt;Reduced repetitive coding tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Unity Developers Love It
&lt;/h3&gt;

&lt;p&gt;Many Unity scripts follow common patterns. Copilot can predict and generate these patterns automatically, significantly reducing development time for gameplay systems, UI management, and editor tools.&lt;/p&gt;

&lt;p&gt;Research and industry studies continue to show growing adoption of AI coding assistants for software development workflows. Developers increasingly use tools like ChatGPT and GitHub Copilot for coding, debugging, and documentation tasks.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Unity ML-Agents: Build Smarter NPCs for Free
&lt;/h2&gt;

&lt;p&gt;If you're interested in creating intelligent game characters, Unity ML-Agents is one of the most powerful free tools available.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is ML-Agents?
&lt;/h3&gt;

&lt;p&gt;ML-Agents is Unity's open-source machine learning toolkit that allows developers to train game agents using reinforcement learning.&lt;/p&gt;

&lt;h3&gt;
  
  
  What You Can Build
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Adaptive enemy AI.&lt;/li&gt;
&lt;li&gt;Self-learning racing cars.&lt;/li&gt;
&lt;li&gt;Intelligent companions.&lt;/li&gt;
&lt;li&gt;Strategy game bots.&lt;/li&gt;
&lt;li&gt;Dynamic NPC behaviors.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why It Matters
&lt;/h3&gt;

&lt;p&gt;Traditional AI relies on predefined rules. ML-Agents enables characters to learn through experience, creating more realistic and engaging gameplay.&lt;/p&gt;

&lt;p&gt;For developers exploring advanced AI systems, ML-Agents remains one of the most valuable free resources in the Unity ecosystem.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Cursor and AI-Powered Code Editors
&lt;/h2&gt;

&lt;p&gt;Modern AI-first code editors are changing how developers write software. Cursor has become particularly popular because it combines traditional coding features with AI-powered project understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features Developers Appreciate
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Understands your entire codebase.&lt;/li&gt;
&lt;li&gt;Generates multi-file code changes.&lt;/li&gt;
&lt;li&gt;Refactors scripts automatically.&lt;/li&gt;
&lt;li&gt;Explains complex code structures.&lt;/li&gt;
&lt;li&gt;Speeds up debugging workflows.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Unity Development Advantages
&lt;/h3&gt;

&lt;p&gt;Large Unity projects often contain hundreds of scripts. AI-powered editors can analyze relationships between files and provide context-aware suggestions that save significant development time.&lt;/p&gt;

&lt;p&gt;Many AI-native editors now offer free plans suitable for solo developers and hobby projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Unity MCP: Bringing AI Directly Into the Unity Editor
&lt;/h2&gt;

&lt;p&gt;A newer trend in game development is connecting AI assistants directly to Unity itself through MCP (Model Context Protocol) integrations.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Unity MCP Can Do
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create GameObjects using natural language.&lt;/li&gt;
&lt;li&gt;Modify scenes automatically.&lt;/li&gt;
&lt;li&gt;Generate scripts directly inside projects.&lt;/li&gt;
&lt;li&gt;Access project context for better AI assistance.&lt;/li&gt;
&lt;li&gt;Trigger editor actions through AI commands.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why It's Exciting
&lt;/h3&gt;

&lt;p&gt;Instead of switching between Unity and external AI tools, developers can interact with AI directly within their development environment.&lt;/p&gt;

&lt;p&gt;Several free and open-source Unity MCP solutions have emerged, allowing developers to connect Unity with tools such as ChatGPT, Claude, Cursor, and GitHub Copilot. These integrations can automate repetitive editor tasks and accelerate prototyping.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;AI is no longer a futuristic concept for game development—it's becoming an essential part of modern Unity workflows. Whether you're generating scripts with ChatGPT, accelerating coding with GitHub Copilot, training intelligent NPCs using ML-Agents, or automating editor tasks through Unity MCP, these tools can dramatically reduce development time.&lt;/p&gt;

&lt;p&gt;The biggest advantage is that you don't need a large budget to get started. Most of these tools provide free tiers, open-source versions, or community-supported solutions that are more than enough for indie developers and small teams.&lt;/p&gt;

&lt;p&gt;By incorporating the right AI tools into your workflow today, you can spend less time on repetitive tasks and more time building great games.&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>developer</category>
      <category>aitools</category>
      <category>freeaitools</category>
    </item>
  </channel>
</rss>
