{"id":31925,"date":"2024-11-01T09:04:15","date_gmt":"2024-11-01T09:04:15","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31925"},"modified":"2024-11-01T11:34:08","modified_gmt":"2024-11-01T11:34:08","slug":"unity-basics-course-elements-and-indexes-of-arrays","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31925\/","title":{"rendered":"Unity Basics Course: Elements and Indexes of Arrays"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>1. What is an Array?<\/h2>\n<p>An array is a data structure that allows you to manage multiple elements of the same data type by grouping them into a single variable. In Unity, arrays are used to easily store and access multiple data (e.g., scores, positions, colors, etc.). Each element of the array can be accessed via an integer index, which starts from 0. The basic structure of an array is as follows:<\/p>\n<pre><code>Type[] arrayName = new Type[arraySize];<\/code><\/pre>\n<p>For example, here is how to declare and initialize an integer array:<\/p>\n<pre><code>int[] scores = new int[5];<\/code><\/pre>\n<\/section>\n<section>\n<h2>2. Accessing Array Elements<\/h2>\n<p>You can access each element of the array using its index. Since the index starts at 0, the first element is accessed as <code>array[0]<\/code>, and the second element as <code>array[1]<\/code>. The way to assign values to array elements is as follows:<\/p>\n<pre><code>scores[0] = 100;<\/code><\/pre>\n<p>Doing this assigns 100 to the first element of the <code>scores<\/code> array. It is also possible to assign values to multiple elements:<\/p>\n<pre><code>scores[1] = 90;\nscores[2] = 80;<\/code><\/pre>\n<\/section>\n<section>\n<h2>3. Checking the Length of an Array<\/h2>\n<p>The length of an array can be checked using <code>arrayName.Length<\/code>. This is useful when you want to know the size of the array. For example, you can output the length of the array as follows:<\/p>\n<pre><code>Debug.Log(\"Array length: \" + scores.Length);<\/code><\/pre>\n<\/section>\n<section>\n<h2>4. How to Initialize an Array<\/h2>\n<p>You can initialize an array at the same time as declaring it. If you want to set the initial values of the array, you can write it like this:<\/p>\n<pre><code>int[] scores = {100, 90, 80, 70, 60};<\/code><\/pre>\n<p>In this case, the elements of the array are initialized to 100, 90, 80, 70, and 60, respectively. If you list values within curly braces without specifying the array size, the size is determined automatically.<\/p>\n<\/section>\n<section>\n<h2>5. Multidimensional Arrays<\/h2>\n<p>Arrays support not only one-dimensional arrays but also multidimensional arrays. The most commonly used multidimensional array is a 2D array, which consists of rows and columns. Here is how to declare a 2D array:<\/p>\n<pre><code>int[,] matrix = new int[3, 3];<\/code><\/pre>\n<p>After declaring it this way, you use the row and column indices to access each element:<\/p>\n<pre><code>matrix[0, 0] = 1;<\/code><\/pre>\n<p>You can also set initial values:<\/p>\n<pre><code>int[,] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };<\/code><\/pre>\n<\/section>\n<section>\n<h2>6. Using Loops with Arrays<\/h2>\n<p>You can use loops to iterate over the elements of an array. The most commonly used loop is the <code>for<\/code> loop. Let&#8217;s look at an example that outputs all elements of the array:<\/p>\n<pre><code>for (int i = 0; i &lt; scores.Length; i++)\n{\n    Debug.Log(\"Score \" + i + \": \" + scores[i]);\n}<\/code><\/pre>\n<p>In the code above, you can see an example of using a <code>for<\/code> loop to print all scores in the array.<\/p>\n<\/section>\n<section>\n<h2>7. Difference Between Arrays and Lists<\/h2>\n<p>In Unity, the data structure most often compared with arrays is List. Lists support variable lengths and make adding and removing data easier. While arrays have a fixed size, Lists can adjust their size as needed. Here is an example of using a List:<\/p>\n<pre><code>using System.Collections.Generic;\n\n\/\/ List declaration\nList<int> scoreList = new List<int>();\n\n\/\/ Adding elements\nscoreList.Add(100);\nscoreList.Add(90);\nscoreList.Add(80);\n<\/code><\/pre>\n<p>If you understand and utilize the characteristics of arrays and Lists well, you can manage data more efficiently in programming.<\/p>\n<\/section>\n<section>\n<h2>8. Useful Methods for Arrays<\/h2>\n<p>Unity provides several useful methods for arrays. One of them, <code>Array.Sort<\/code>, is used to sort an array.<\/p>\n<pre><code>Array.Sort(scores);<\/code><\/pre>\n<p>This code sorts the elements of the <code>scores<\/code> array in ascending order. Additionally, you can use the <code>Array.Reverse<\/code> method to reverse the array.<\/p>\n<pre><code>Array.Reverse(scores);<\/code><\/pre>\n<\/section>\n<section>\n<h2>9. Copying Arrays<\/h2>\n<p>To copy an array, you can use the <code>Array.Copy<\/code> method. This method is useful for copying the elements of a source array to a new array.<\/p>\n<pre><code>int[] copiedScores = new int[scores.Length];\nArray.Copy(scores, copiedScores, scores.Length);<\/code><\/pre>\n<p>This will initialize the <code>copiedScores<\/code> array with the elements of the <code>scores<\/code> array.<\/p>\n<\/section>\n<section>\n<h2>10. Working with Arrays<\/h2>\n<p>When using arrays, one thing to be mindful of is to avoid accessing out of bounds of the array&#8217;s index range. Accessing an index greater than the length of the array will result in an <code>IndexOutOfRangeException<\/code>. It is always a good idea to check the length of the array.<\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we explored the basic concepts and usage of arrays in Unity. Arrays are a powerful tool for effective data management and can be utilized in various ways. By understanding and utilizing other data structures like Lists along with arrays, you can write more efficient code. Be sure to actively use arrays in your future projects!<\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. What is an Array? An array is a data structure that allows you to manage multiple elements of the same data type by grouping them into a single variable. In Unity, arrays are used to easily store and access multiple data (e.g., scores, positions, colors, etc.). Each element of the array can be accessed &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31925\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Unity Basics Course: Elements and Indexes of Arrays&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[135],"tags":[],"class_list":["post-31925","post","type-post","status-publish","format-standard","hentry","category-unity-basic"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unity Basics Course: Elements and Indexes of Arrays - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31925\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unity Basics Course: Elements and Indexes of Arrays - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. What is an Array? An array is a data structure that allows you to manage multiple elements of the same data type by grouping them into a single variable. In Unity, arrays are used to easily store and access multiple data (e.g., scores, positions, colors, etc.). Each element of the array can be accessed &hellip; \ub354 \ubcf4\uae30 &quot;Unity Basics Course: Elements and Indexes of Arrays&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31925\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:04:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:34:08+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31925\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31925\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Unity Basics Course: Elements and Indexes of Arrays\",\"datePublished\":\"2024-11-01T09:04:15+00:00\",\"dateModified\":\"2024-11-01T11:34:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31925\/\"},\"wordCount\":630,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31925\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31925\/\",\"name\":\"Unity Basics Course: Elements and Indexes of Arrays - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:04:15+00:00\",\"dateModified\":\"2024-11-01T11:34:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31925\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31925\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31925\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unity Basics Course: Elements and Indexes of Arrays\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unity Basics Course: Elements and Indexes of Arrays - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31925\/","og_locale":"ko_KR","og_type":"article","og_title":"Unity Basics Course: Elements and Indexes of Arrays - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. What is an Array? An array is a data structure that allows you to manage multiple elements of the same data type by grouping them into a single variable. In Unity, arrays are used to easily store and access multiple data (e.g., scores, positions, colors, etc.). Each element of the array can be accessed &hellip; \ub354 \ubcf4\uae30 \"Unity Basics Course: Elements and Indexes of Arrays\"","og_url":"https:\/\/atmokpo.com\/w\/31925\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:04:15+00:00","article_modified_time":"2024-11-01T11:34:08+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31925\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31925\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Unity Basics Course: Elements and Indexes of Arrays","datePublished":"2024-11-01T09:04:15+00:00","dateModified":"2024-11-01T11:34:08+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31925\/"},"wordCount":630,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31925\/","url":"https:\/\/atmokpo.com\/w\/31925\/","name":"Unity Basics Course: Elements and Indexes of Arrays - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:04:15+00:00","dateModified":"2024-11-01T11:34:08+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31925\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31925\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31925\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Unity Basics Course: Elements and Indexes of Arrays"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31925"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31925\/revisions"}],"predecessor-version":[{"id":31926,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31925\/revisions\/31926"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}