{"id":32113,"date":"2024-11-01T09:05:46","date_gmt":"2024-11-01T09:05:46","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32113"},"modified":"2024-11-01T11:33:15","modified_gmt":"2024-11-01T11:33:15","slug":"unity-basics-course-creating-classes","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32113\/","title":{"rendered":"Unity Basics Course: Creating Classes"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Unity is a powerful engine for game development that uses the C# language to create games. C# supports Object-Oriented Programming (OOP), which helps programmers write code efficiently and in a reusable manner. This article will explain how to create classes in Unity in detail.\n    <\/p>\n<h2>1. Understanding C# and Object-Oriented Programming<\/h2>\n<p>\n        A class is the fundamental unit of object-oriented programming, defining data and the functions that process that data together. In C#, the following basic concepts exist:\n    <\/p>\n<ul>\n<li><strong>Class<\/strong>: A blueprint for creating objects.<\/li>\n<li><strong>Object<\/strong>: An instance of a class.<\/li>\n<li><strong>Property<\/strong>: Data defined in the class.<\/li>\n<li><strong>Method<\/strong>: A function defined in the class.<\/li>\n<li><strong>Constructor<\/strong>: A special method that is called when an instance of a class is created.<\/li>\n<\/ul>\n<h2>2. Creating a Unity Project<\/h2>\n<p>\n        Before starting to write a class in Unity, you must first create a Unity project. Create a new project in Unity Hub and select the basic 3D template. Once the project is loaded, follow the steps below.\n    <\/p>\n<h2>3. Creating a Class<\/h2>\n<p>\n        Right-click in the project&#8217;s <strong>Assets<\/strong> folder and select <strong>Create &gt; C# Script<\/strong> to create a new script. Name the script <strong>MyFirstClass<\/strong>. Double click on the script to open it in Visual Studio or your preferred code editor.\n    <\/p>\n<pre><code>\n    using UnityEngine;\n\n    public class MyFirstClass\n    {\n        \/\/ Properties\n        public int health;\n        public string playerName;\n\n        \/\/ Constructor\n        public MyFirstClass(string name, int initialHealth)\n        {\n            playerName = name;\n            health = initialHealth;\n        }\n\n        \/\/ Method\n        public void TakeDamage(int damage)\n        {\n            health -= damage;\n            if (health < 0)\n                health = 0;\n\n            Debug.Log(playerName + \"'s remaining health: \" + health);\n        }\n    }\n    <\/code><\/pre>\n<h3>3.1. Class Explanation<\/h3>\n<p>\n        In the above code, <strong>MyFirstClass<\/strong> is used to manage the player's health and name. The properties are <strong>health<\/strong> and <strong>playerName<\/strong>, which are initialized through the constructor when creating an instance. The <strong>TakeDamage<\/strong> method reduces health when damage is taken and logs the result.\n    <\/p>\n<h2>4. Using the Class<\/h2>\n<p>\n        To use a class in Unity, you need to create an instance in another script. Let's create an instance of the <strong>MyFirstClass<\/strong> class:\n    <\/p>\n<pre><code>\n    using UnityEngine;\n\n    public class GameManager : MonoBehaviour\n    {\n        void Start()\n        {\n            MyFirstClass player = new MyFirstClass(\"Player1\", 100);\n            player.TakeDamage(20);\n        }\n    }\n    <\/code><\/pre>\n<h3>4.1. GameManager Class Explanation<\/h3>\n<p>\n<strong>GameManager<\/strong> class is a script that inherits from Unity's <strong>MonoBehaviour<\/strong>. The <strong>Start<\/strong> method is called when the script starts, and here we create an instance of <strong>MyFirstClass<\/strong> and call the <strong>TakeDamage<\/strong> method to reduce health.\n    <\/p>\n<h2>5. Working with the Unity Editor<\/h2>\n<p>\n        Now, return to the Unity Editor and add the <strong>GameManager<\/strong> script to an empty game object. Create an empty game object and drag the <strong>GameManager<\/strong> script onto it. Now, press the play button to check if the player's health is logged correctly.\n    <\/p>\n<h2>6. Expanding the Class<\/h2>\n<p>\n        Let's expand the class by adding various functionalities. For example, let's add a <strong>Heal<\/strong> method so the player can recover health:\n    <\/p>\n<pre><code>\n    public void Heal(int amount)\n    {\n        health += amount;\n        if (health > 100) \/\/ Maximum health is capped at 100\n            health = 100;\n\n        Debug.Log(playerName + \"'s current health: \" + health);\n    }\n    <\/code><\/pre>\n<h3>6.1. Using the Heal Method<\/h3>\n<p>\nTo use the <strong>Heal<\/strong> method, we will call it from the <strong>GameManager<\/strong> class:\n    <\/p>\n<pre><code>\n    player.Heal(30);\n    <\/code><\/pre>\n<h2>7. Understanding Classes and Inheritance<\/h2>\n<p>\n        Inheritance is one of the important concepts in object-oriented programming. You can inherit a class and add new functionalities. For example, let's create a <strong>Warrior<\/strong> class that inherits from <strong>MyFirstClass<\/strong>:\n    <\/p>\n<pre><code>\n    public class Warrior : MyFirstClass\n    {\n        public int attackPower;\n\n        public Warrior(string name, int initialHealth, int initialAttackPower) : base(name, initialHealth)\n        {\n            attackPower = initialAttackPower;\n        }\n\n        public void Attack()\n        {\n            Debug.Log(playerName + \" deals \" + attackPower + \" damage!\");\n        }\n    }\n    <\/code><\/pre>\n<h3>7.1. Using the Warrior Class<\/h3>\n<p>\nLet's create an instance of the <strong>Warrior<\/strong> class and call the attack method:\n    <\/p>\n<pre><code>\n    Warrior warrior = new Warrior(\"Warrior\", 120, 50);\n    warrior.Attack();\n    <\/code><\/pre>\n<h2>8. Utilizing Classes<\/h2>\n<p>\n        The ways to utilize classes in Unity are limitless. For example, enemy characters, weapons, items, etc. can be created as classes, each defining their own properties and methods to express various behaviors.\n    <\/p>\n<h2>9. Conclusion<\/h2>\n<p>\n        In this tutorial, we explored how to create classes in Unity and the basic principles of object-oriented programming in C#. By utilizing classes, you can write more structured and maintainable code. Continue to understand various classes and inheritance, and enhance your skills when creating games.\n    <\/p>\n<h2>10. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.unity3d.com\/Manual\/ScriptReference\/MonoBehaviour.html\" target=\"_blank\" rel=\"noopener\">Unity MonoBehaviour Documentation<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/ko-kr\/dotnet\/csharp\/programming-guide\/classes-and-structs\/classes\" target=\"_blank\" rel=\"noopener\">C# Classes and Structs<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unity is a powerful engine for game development that uses the C# language to create games. C# supports Object-Oriented Programming (OOP), which helps programmers write code efficiently and in a reusable manner. This article will explain how to create classes in Unity in detail. 1. Understanding C# and Object-Oriented Programming A class is the fundamental &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32113\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Unity Basics Course: Creating Classes&#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-32113","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: Creating Classes - \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\/32113\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unity Basics Course: Creating Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Unity is a powerful engine for game development that uses the C# language to create games. C# supports Object-Oriented Programming (OOP), which helps programmers write code efficiently and in a reusable manner. This article will explain how to create classes in Unity in detail. 1. Understanding C# and Object-Oriented Programming A class is the fundamental &hellip; \ub354 \ubcf4\uae30 &quot;Unity Basics Course: Creating Classes&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32113\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:05:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:33:15+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32113\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32113\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Unity Basics Course: Creating Classes\",\"datePublished\":\"2024-11-01T09:05:46+00:00\",\"dateModified\":\"2024-11-01T11:33:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32113\/\"},\"wordCount\":546,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Unity Basic\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32113\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32113\/\",\"name\":\"Unity Basics Course: Creating Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:05:46+00:00\",\"dateModified\":\"2024-11-01T11:33:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32113\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32113\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32113\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unity Basics Course: Creating Classes\"}]},{\"@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: Creating Classes - \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\/32113\/","og_locale":"ko_KR","og_type":"article","og_title":"Unity Basics Course: Creating Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Unity is a powerful engine for game development that uses the C# language to create games. C# supports Object-Oriented Programming (OOP), which helps programmers write code efficiently and in a reusable manner. This article will explain how to create classes in Unity in detail. 1. Understanding C# and Object-Oriented Programming A class is the fundamental &hellip; \ub354 \ubcf4\uae30 \"Unity Basics Course: Creating Classes\"","og_url":"https:\/\/atmokpo.com\/w\/32113\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:05:46+00:00","article_modified_time":"2024-11-01T11:33:15+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32113\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32113\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Unity Basics Course: Creating Classes","datePublished":"2024-11-01T09:05:46+00:00","dateModified":"2024-11-01T11:33:15+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32113\/"},"wordCount":546,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Unity Basic"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32113\/","url":"https:\/\/atmokpo.com\/w\/32113\/","name":"Unity Basics Course: Creating Classes - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:05:46+00:00","dateModified":"2024-11-01T11:33:15+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32113\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32113\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32113\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Unity Basics Course: Creating Classes"}]},{"@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\/32113","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=32113"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32113\/revisions"}],"predecessor-version":[{"id":32114,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32113\/revisions\/32114"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}