{"id":31761,"date":"2024-11-01T09:02:37","date_gmt":"2024-11-01T09:02:37","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31761"},"modified":"2024-11-01T11:34:50","modified_gmt":"2024-11-01T11:34:50","slug":"basic-unity-course-creating-variables","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31761\/","title":{"rendered":"Basic Unity Course: Creating Variables"},"content":{"rendered":"<p><body><\/p>\n<p>Unity is a popular platform for game and interactive content development. It offers numerous features and tools, allowing you to write scripts using C#. In this tutorial, we will learn about creating variables in Unity. Variables serve as storage for data and are a core element of programming. By creating variables correctly, we can more effectively structure the logic of our games.<\/p>\n<h2>1. What is a variable?<\/h2>\n<p>A variable is a named space in memory for storing data. It allows you to store a specific value and reference it whenever needed. Variables can hold various forms of values, and these values can change during the execution of the program.<\/p>\n<h3>1.1 The necessity of variables<\/h3>\n<p>Variables are very important in programming. For example, they are used to store and manage the player&#8217;s score, status, position, etc., in a game. Without the use of variables, tracking and managing the state of the game becomes difficult.<\/p>\n<h3>1.2 Types of variables<\/h3>\n<p>Common types of variables used in Unity and C# include:<\/p>\n<ul>\n<li><strong>Integer (int)<\/strong>: Stores integer values. Example: score, lives<\/li>\n<li><strong>Float (float)<\/strong>: Stores decimal values. Example: character speed<\/li>\n<li><strong>String (string)<\/strong>: Stores text data. Example: player name<\/li>\n<li><strong>Boolean (bool)<\/strong>: Stores true or false values. Example: whether dead or alive<\/li>\n<li><strong>Array (Array)<\/strong>: A data structure that can store multiple values of the same data type.<\/li>\n<\/ul>\n<h2>2. Creating variables in Unity<\/h2>\n<p>Creating variables is very simple. It involves declaring and initializing (assigning a value) variables within a C# script.<\/p>\n<h3>2.1 How to declare a variable<\/h3>\n<p>To declare a variable, you use the data type and the variable name. The basic syntax is as follows:<\/p>\n<pre><code>dataType variableName;<\/code><\/pre>\n<p>For example, to declare an integer variable:<\/p>\n<pre><code>int playerScore;<\/code><\/pre>\n<h3>2.2 How to initialize a variable<\/h3>\n<p>After declaring a variable, you must initialize it with a value. Initialization can be done right when declaring the variable:<\/p>\n<pre><code>int playerScore = 0;<\/code><\/pre>\n<p>Alternatively, you can assign a value later:<\/p>\n<pre><code>playerScore = 10;<\/code><\/pre>\n<h3>2.3 Declaring and initializing multiple variables<\/h3>\n<p>You can declare and initialize multiple variables at the same time. For example:<\/p>\n<pre><code>int playerHealth = 100, playerLevel = 1;<\/code><\/pre>\n<h2>3. Using variables<\/h2>\n<p>Now that you know how to create variables, let&#8217;s learn how to use the created variables. You can access a variable using its name.<\/p>\n<h3>3.1 Outputting<\/h3>\n<p>You can use the <code>Debug.Log<\/code> method to output the value of a variable. For example:<\/p>\n<pre><code>Debug.Log(playerScore);<\/code><\/pre>\n<h3>3.2 Changing variable values<\/h3>\n<p>To change the value of a variable, simply assign a new value to it:<\/p>\n<pre><code>playerScore += 5; \/\/ Add 5 points<\/code><\/pre>\n<h2>4. Access modifiers and variables<\/h2>\n<p>In C#, access modifiers are used to define how variables can be accessed from other code. Commonly used modifiers include <code>public<\/code>, <code>private<\/code>, and <code>protected<\/code>.<\/p>\n<h3>4.1 Public variables<\/h3>\n<p>Using the <code>public<\/code> keyword allows you to declare a variable that can be accessed from other scripts:<\/p>\n<pre><code>public int playerScore;<\/code><\/pre>\n<h3>4.2 Private variables<\/h3>\n<p>Using the <code>private<\/code> keyword restricts access to the same class only:<\/p>\n<pre><code>private int playerHealth = 100;<\/code><\/pre>\n<h3>4.3 Getters and setters<\/h3>\n<p>To control access to a variable, you can create getters and setters. For example:<\/p>\n<pre><code>private int playerLevel;\n    \n    public int PlayerLevel\n    {\n        get { return playerLevel; }\n        set { playerLevel = value; }\n    }<\/code><\/pre>\n<h2>5. Exposing variables using `SerializeField`<\/h2>\n<p>If you want to expose a variable in the Unity editor, you can use the <code>[SerializeField]<\/code> attribute. Here is an example:<\/p>\n<pre><code>[SerializeField] private int playerHealth = 100;<\/code><\/pre>\n<p>This will allow you to modify the <code>playerHealth<\/code> variable in the Unity editor&#8217;s inspector.<\/p>\n<h2>6. Example code: Simple player script<\/h2>\n<p>Based on what we have learned so far, let&#8217;s write a simple player script. This script will increase the player&#8217;s score and output the current score.<\/p>\n<pre><code>using UnityEngine;\n\n    public class Player : MonoBehaviour\n    {\n        public int playerScore = 0;\n\n        void Start()\n        {\n            Debug.Log(\"Game started! Current score: \" + playerScore);\n        }\n\n        void Update()\n        {\n            if (Input.GetKeyDown(KeyCode.Space))\n            {\n                playerScore += 10;\n                Debug.Log(\"Score increased! Current score: \" + playerScore);\n            }\n        }\n    }<\/code><\/pre>\n<h2>7. Variable scope<\/h2>\n<p>The scope of a variable differs based on where it is declared. Variables can be used in various areas, such as within methods or within classes, and this scope defines where the variable can be accessed. For example, a variable declared within a class can be used in all methods of that class, while a variable declared inside a method can be used only within that method.<\/p>\n<h3>7.1 Local variables<\/h3>\n<p>A variable declared within a method is called a local variable and can only be used within that block:<\/p>\n<pre><code>void SomeMethod()\n    {\n        int localVariable = 5; \/\/ Local variable\n        Debug.Log(localVariable);\n    }<\/code><\/pre>\n<h3>7.2 Instance variables and static variables<\/h3>\n<p>Variables can be categorized into three types: instance variables, static variables, and local variables.<\/p>\n<h4>Instance variables<\/h4>\n<p>Instance variables exist separately for each instance of a class and are declared as follows:<\/p>\n<pre><code>public class ExampleClass\n    {\n        public int instanceVariable; \/\/ Instance variable\n    }<\/code><\/pre>\n<h4>Static variables<\/h4>\n<p>Static variables belong to the class and are shared among all instances, declared as follows:<\/p>\n<pre><code>public class ExampleClass\n    {\n        public static int staticVariable; \/\/ Static variable\n    }<\/code><\/pre>\n<h3>8. Variables and memory<\/h3>\n<p>Understanding memory management is also important when using variables. Variables are stored in specific areas of memory, and this region is allocated differently based on each data type. For example, the <code>int<\/code> type generally uses 4 bytes of memory, and the <code>float<\/code> type also uses 4 bytes. However, reference type data, such as strings, requires dynamic memory allocation.<\/p>\n<h2>9. The importance of variable initialization<\/h2>\n<p>You must initialize a variable before using it. Using an uninitialized variable can cause unexpected behavior, making debugging difficult. Therefore, it is always a good habit to initialize variables with reasonable initial values after declaring them.<\/p>\n<h2>10. Conclusion<\/h2>\n<p>We have learned how to create and use variables in Unity in detail. Variables are a core element of data processing, and when utilized well, they can make the logic of a game much richer and more efficient. Understanding the various data types and structures, as well as declaring and initializing variables appropriately based on the types needed, forms the basis of programming. With this foundation, we hope you can acquire more advanced game development skills.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unity is a popular platform for game and interactive content development. It offers numerous features and tools, allowing you to write scripts using C#. In this tutorial, we will learn about creating variables in Unity. Variables serve as storage for data and are a core element of programming. By creating variables correctly, we can more &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31761\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Basic Unity Course: Creating Variables&#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-31761","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>Basic Unity Course: Creating Variables - \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\/31761\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Basic Unity Course: Creating Variables - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Unity is a popular platform for game and interactive content development. It offers numerous features and tools, allowing you to write scripts using C#. In this tutorial, we will learn about creating variables in Unity. Variables serve as storage for data and are a core element of programming. By creating variables correctly, we can more &hellip; \ub354 \ubcf4\uae30 &quot;Basic Unity Course: Creating Variables&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31761\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:02:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:34:50+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=\"5\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31761\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31761\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Basic Unity Course: Creating Variables\",\"datePublished\":\"2024-11-01T09:02:37+00:00\",\"dateModified\":\"2024-11-01T11:34:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31761\/\"},\"wordCount\":844,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31761\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31761\/\",\"name\":\"Basic Unity Course: Creating Variables - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:02:37+00:00\",\"dateModified\":\"2024-11-01T11:34:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31761\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31761\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31761\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Basic Unity Course: Creating Variables\"}]},{\"@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":"Basic Unity Course: Creating Variables - \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\/31761\/","og_locale":"ko_KR","og_type":"article","og_title":"Basic Unity Course: Creating Variables - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Unity is a popular platform for game and interactive content development. It offers numerous features and tools, allowing you to write scripts using C#. In this tutorial, we will learn about creating variables in Unity. Variables serve as storage for data and are a core element of programming. By creating variables correctly, we can more &hellip; \ub354 \ubcf4\uae30 \"Basic Unity Course: Creating Variables\"","og_url":"https:\/\/atmokpo.com\/w\/31761\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:02:37+00:00","article_modified_time":"2024-11-01T11:34:50+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":"5\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31761\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31761\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Basic Unity Course: Creating Variables","datePublished":"2024-11-01T09:02:37+00:00","dateModified":"2024-11-01T11:34:50+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31761\/"},"wordCount":844,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31761\/","url":"https:\/\/atmokpo.com\/w\/31761\/","name":"Basic Unity Course: Creating Variables - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:02:37+00:00","dateModified":"2024-11-01T11:34:50+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31761\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31761\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31761\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Basic Unity Course: Creating Variables"}]},{"@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\/31761","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=31761"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31761\/revisions"}],"predecessor-version":[{"id":31762,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31761\/revisions\/31762"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}