<?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: Andy</title>
    <description>The latest articles on DEV Community by Andy (@andytq).</description>
    <link>https://dev.to/andytq</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F360224%2F64d239fe-c33c-4278-bbb4-ceab089cc241.jpeg</url>
      <title>DEV Community: Andy</title>
      <link>https://dev.to/andytq</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andytq"/>
    <language>en</language>
    <item>
      <title>Unity: Comparison of performance cost between "GameObject.Find" and direct referencing</title>
      <dc:creator>Andy</dc:creator>
      <pubDate>Sat, 04 Apr 2020 02:16:56 +0000</pubDate>
      <link>https://dev.to/andytq/unity-comparison-of-performance-cost-between-gameobject-find-and-direct-referencing-pb</link>
      <guid>https://dev.to/andytq/unity-comparison-of-performance-cost-between-gameobject-find-and-direct-referencing-pb</guid>
      <description>&lt;p&gt;When I was developing Unity games, I had learned that &lt;code&gt;GameObject.Find&lt;/code&gt; is slow once the size of object becomes bigger. Also, many game developers suggested against using such method, as it not only slows down the program, but also impacts the overall maintainability of the code. But I have never get a chance to quantify how &lt;em&gt;slow&lt;/em&gt; will the game become if we exploit the usage of &lt;code&gt;GameObject.Find&lt;/code&gt;. Therefore, it is a good time to perform an experiment on these two methods. In this article, I will do a very simple experiment on the following methods. All of these methods perform the same action: Getting a reference to a game object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;GameObject.Find&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Set up the public variable in the script, then drag the other game object to the reference box in Unity Editor.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The development environment I am on is 2019.3.7.f1. I will perform the scripting in this experiment using C#. Here is how I set up the structure of the experiment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Independent variable: The method of accessing the reference to a &lt;code&gt;Cube&lt;/code&gt; game object&lt;/li&gt;
&lt;li&gt;Dependent variable: The time elapsed after scanning through all the &lt;code&gt;Cube&lt;/code&gt; game object using the given method&lt;/li&gt;
&lt;li&gt;The CPU, RAM usage, and other game objects (&lt;code&gt;Main Camera&lt;/code&gt;, &lt;code&gt;Directional Light&lt;/code&gt;) are controlled and won't change. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;p&gt;Firstly, I created a new Unity project, as usual. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZkMq3dFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4wbfu75m1a21o16fgdmv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZkMq3dFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4wbfu75m1a21o16fgdmv.png" alt="Alt Text" width="880" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My brand new project looked as empty as this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C_IMVEOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xpo8xhts6d6tj8jeiwmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C_IMVEOv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xpo8xhts6d6tj8jeiwmo.png" alt="Alt Text" width="754" height="842"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Testing the performance of direct referencing
&lt;/h1&gt;

&lt;p&gt;To start testing, it's time to write some scripts -- under &lt;code&gt;Assets&lt;/code&gt; folder, I created  &lt;code&gt;scripts&lt;/code&gt; directory and then added a new script, as you can see from the screen shot below:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--as4P2Vjm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sak3j2jdfj16vyij20v2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--as4P2Vjm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/sak3j2jdfj16vyij20v2.png" alt="Alt Text" width="442" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is how the test is implemented. To test the performance of direct referencing an object in Editor, we firstly create 10,000 GameObjects using the following 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="nn"&gt;System.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ExecuteInEditMode&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;FindTest&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;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&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;cubes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&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;size&lt;/span&gt;&lt;span class="p"&gt;];&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;cubes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&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;size&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="p"&gt;=&lt;/span&gt; &lt;span class="m"&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="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++){&lt;/span&gt;
            &lt;span class="n"&gt;GameObject&lt;/span&gt; &lt;span class="n"&gt;cube&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;CreatePrimitive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PrimitiveType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Cube&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;cubes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cube&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;To use this script, simply create an empty game object. Then, on the right side, search for the &lt;code&gt;Create&lt;/code&gt; script. Once you load the &lt;code&gt;Create&lt;/code&gt; script, 10,000 cubes will pop up in the editor, and they are all binded to the &lt;code&gt;cubes&lt;/code&gt; array in &lt;code&gt;Create&lt;/code&gt; class.&lt;br&gt;
Once we finished creating these GameObjects, the editor will look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Knag4bLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eovd19d8n23fe3qm56d2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Knag4bLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/eovd19d8n23fe3qm56d2.png" alt="Alt Text" width="880" height="658"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since each cube has a &lt;code&gt;BoxCollider&lt;/code&gt; component, our goal is to turn off the &lt;code&gt;BoxCollider&lt;/code&gt; of all the components. We will measure how long does this action take.&lt;/p&gt;

&lt;p&gt;We use the following script to perform the action, which is modified based upon the previous script: We removed the procedure for &lt;em&gt;creating&lt;/em&gt; the cubes, and replaced them with &lt;code&gt;cubes[i].GetComponent&amp;lt;BoxCollider&amp;gt;().enabled = false&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="nn"&gt;System.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;Create&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;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&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;cubes&lt;/span&gt;&lt;span class="p"&gt;;&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;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="p"&gt;=&lt;/span&gt; &lt;span class="m"&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="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++){&lt;/span&gt;
            &lt;span class="n"&gt;cubes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BoxCollider&lt;/span&gt;&lt;span class="p"&gt;&amp;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="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="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;realtimeSinceStartup&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;Once Unity finishes the action, the &lt;strong&gt;time elapsed (in seconds)&lt;/strong&gt; will be sent to the console. We perform the same action 10 times. The console will then look like this, if we turn off &lt;code&gt;clear on play&lt;/code&gt; and all other options. The console will collect all the data once we finish the experiment. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--74ELLISQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qdt4nbjxrywh8oqx2aw6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--74ELLISQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qdt4nbjxrywh8oqx2aw6.png" alt="Alt Text" width="760" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We extracted the dataset into a spreadsheet.&lt;br&gt;
The following table shows the result after this first experiment:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h2UsPUHV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2ukwmm4grq25a7afhgp0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h2UsPUHV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2ukwmm4grq25a7afhgp0.png" alt="Alt Text" width="414" height="902"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us move on to the second test, to see how does &lt;code&gt;GameObject.Find()&lt;/code&gt; performs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Testing &lt;code&gt;GameObject.Find()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now let us test the performance of &lt;code&gt;GameObject.Find()&lt;/code&gt;. We can use the testing scripts we developed previously, with some slight modifications. In fact, we only modified one line -- changing from &lt;code&gt;cubes[i].GetComponent&amp;lt;BoxCollider&amp;gt;().enabled = false;&lt;/code&gt; to &lt;code&gt;GameObject.Find(i.ToString()).GetComponent&amp;lt;BoxCollider&amp;gt;().enabled = false;&lt;/code&gt;. This will change the way we access to the Cube objects.&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;System.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&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;Create&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;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&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;cubes&lt;/span&gt;&lt;span class="p"&gt;;&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;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="p"&gt;=&lt;/span&gt; &lt;span class="m"&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="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&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;Find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;BoxCollider&lt;/span&gt;&lt;span class="p"&gt;&amp;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="c1"&gt;// Modified line&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="n"&gt;Time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;realtimeSinceStartup&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;After performing the test 10 times (same as before), we recorded the following data -- indeed, as we can see, &lt;code&gt;GameObject.Find()&lt;/code&gt; took longer in accessing all the game objects and turn their &lt;code&gt;BoxCollider&lt;/code&gt;s off.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JgdgQC-h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c7hzgrfi515ttys7aemd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JgdgQC-h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/c7hzgrfi515ttys7aemd.png" alt="Alt Text" width="484" height="952"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Testing on a smaller number of game objects
&lt;/h1&gt;

&lt;p&gt;We have seen that &lt;code&gt;GameObject.Find()&lt;/code&gt; significantly lags behind when there is a large number of GameObjects in the scene. But what if we change the number of objects into a smaller one, e.g. 100? Let's do the same experiment with the smaller number. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XP9mxO78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m9pz3lytc3ujyih955ah.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XP9mxO78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m9pz3lytc3ujyih955ah.jpg" alt="Alt Text" width="880" height="495"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t84pgSAh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ku6585817s3sw9en8k9z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t84pgSAh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ku6585817s3sw9en8k9z.png" alt="Alt Text" width="880" height="813"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see from the figure above there isn't a huge time difference between these two methods if we reduce the number of game objects in the experiment -- &lt;code&gt;GameObject.Find&lt;/code&gt; even has a tiny edge over direct referencing in this smaller experiment. After gathering all the data, we produced the following figure, whereas each bar represents the average time of using the corresponding method:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--42UxQMfb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1of7demrcicsiqajiz1a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--42UxQMfb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1of7demrcicsiqajiz1a.png" alt="Alt Text" width="880" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;In conclusion, we can discover that &lt;code&gt;GameObject.Find&lt;/code&gt; has worse performance comparing to direct referencing in the experiment. This is potentially due to the fact that &lt;code&gt;GameObject.Find&lt;/code&gt; needs to linearly find the object with corresponding name, whereas the direct referencing method does not need to perform linear search. Overall, if one doesn't &lt;em&gt;really need to&lt;/em&gt; use &lt;code&gt;GameObject.Find&lt;/code&gt;, it should be avoided. &lt;/p&gt;

&lt;p&gt;However, in this experiment, we have only explored two of the many methods for referencing a game object in Unity. There are many methods that we can also include in this experiment. For instance, we have not tried &lt;code&gt;FindObjectOfType&lt;/code&gt; and &lt;code&gt;FindGameObjectsWithTag&lt;/code&gt; yet. These are the methods that we can include in the future experiments. &lt;/p&gt;

</description>
      <category>unity3d</category>
    </item>
  </channel>
</rss>
