{"id":855,"date":"2019-12-11T23:26:43","date_gmt":"2019-12-11T23:26:43","guid":{"rendered":"https:\/\/www.danielparente.net\/en\/2019\/12\/11\/methods-in-c-programming-for-unity-game-development-game-development-blog\/"},"modified":"2019-12-11T23:26:43","modified_gmt":"2019-12-11T23:26:43","slug":"methods-in-c-programming-for-unity-game-development-game-development-blog","status":"publish","type":"post","link":"https:\/\/www.danielparente.net\/en\/2019\/12\/11\/methods-in-c-programming-for-unity-game-development-game-development-blog\/","title":{"rendered":"Methods in C# \u2013 Programming for Unity Game Development \u2013 Game Development Blog"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div>\n<blockquote class=\"wp-block-quote\">\n<p>\u201cIt is the method behind the madness that makes it madness.\u2019<\/p>\n<p><cite> \u2015 Marty Rubin<\/cite><\/p><\/blockquote>\n<p><span id=\"more-949\"\/><\/p>\n<blockquote class=\"wp-block-coblocks-click-to-tweet\">\n<p class=\"wp-block-coblocks-click-to-tweet__text\">Do you want to be a game developer? Learn C# programming fundamentals in Unity with this free tutorial by @MammothCompany! All about methods\u2026.Where would we be without them?<\/p>\n<p><a class=\"wp-block-coblocks-click-to-tweet__twitter-btn\" href=\"http:\/\/twitter.com\/share?&amp;text=Do%20you%20want%20to%20be%20a%20game%20developer%3F%20Learn%20C%23%20programming%20fundamentals%20in%20Unity%20with%20this%20free%20tutorial%20by%20%40MammothCompany!%20All%20about%20methods....Where%20would%20we%20be%20without%20them%3F&amp;url=http:\/\/www.gamedevelopmentblog.com\/2019\/12\/methods-in-csharp-programming-for-unity-game-development\/\" target=\"_blank\" rel=\"noopener noreferrer\">Tweet<\/a><\/p><\/blockquote>\n<h2>In this tutorial:<\/h2>\n<h2>Setup<\/h2>\n<p>To follow along with this tutorial, you\u2019ll need to <strong><a href=\"http:\/\/www.gamedevelopmentblog.com\/2019\/11\/unityeditor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">open a new Unity project<\/a><\/strong>.<\/p>\n<blockquote class=\"wp-block-quote\">\n<p>Sure, Unity gives us two default methods in each new script we make. But to make a game, we\u2019re going to need a lot more than that. <\/p>\n<\/blockquote>\n<h2 id=\"intro\"><strong>Part 1: What Is A Method? <\/strong><\/h2>\n<p>A method is a task or function that you can<br \/>\nreuse throughout code simply by calling its name. <\/p>\n<p>But before you can call a method, you must declare it and set it up, including any <strong>parameters <\/strong>(data that the method will use to perform its task) or <strong>return values<\/strong> (the value that the method will spit out when it has completed its task, usually to determine that value.) <\/p>\n<p>Making a new method also allows you to<br \/>\nreuse it easily by simply calling its name.<\/p>\n<p>The best way to learn is by doing. So, let\u2019s make a new method for a Unity game.<\/p>\n<h2 id=\"method\">Part 2: Making a C# Method<\/h2>\n<p>Open the script you made that controls your game, typically called GameController.cs.  If you don\u2019t have a Game Controller yet, check out our previous tutorial.  <\/p>\n<p>Type the following line below the Update method to create a method named PrintMessage.<\/p>\n<p><code>void PrintMessage () {<\/code><\/p>\n<p>}<\/p>\n<p>The method\u2019s name is <strong>PrintMessage<\/strong>. Notice<br \/>\nthat the first letter of each word in the name begins with a capital letter,<br \/>\nbut the remaining letters are lowercase. This is the language convention you<br \/>\nshould follow whenever creating a method in C#. <\/p>\n<p><strong>void<\/strong> means<br \/>\nthat the method will not return a value. Because the <strong>parentheses<\/strong> are<br \/>\nempty, the method receives no parameters. There is no return statement using<br \/>\nthe return keyword, so (at least for now) the method does not return any value.<\/p>\n<p>The code we put between the curly brackets<br \/>\nis the <strong>scope<\/strong> of the method. This is where we write the task that we want<br \/>\nthe method to perform whenever its name is called.<\/p>\n<p>Let\u2019s call the Debug class\u2019s Log method to print a string and the sum of two variables we created in the previous tutorial. <\/p>\n<p><code>void PrintMessage () {<br \/><\/code>          <code><strong>Debug.Log (\"The sum is: \" + (number1 + number2));<br \/><\/strong>}<\/code><\/p>\n<p>Now, whenever we call PrintMessage (), the<br \/>\nLog method inside PrintMessage () will be executed as we specified.<\/p>\n<h2 id=\"test\">Part 3: Testing Methods in Unity<\/h2>\n<p>Save the script, and open Unity. Press<br \/>\nPlay. Only \u201cLet\u2019s go!\u201d will print in the Console. This occurs because<br \/>\nwe did not call the PrintMessage method. We need to not only create a method<br \/>\nbut also call it. <\/p>\n<p>Press Stop. Open GameController.cs. Use the following code to call PrintMessage in the Start method, along with the message variable we created last tutorial.<\/p>\n<p><code>void Start () {<br \/><\/code>            <code>Debug.Log (message);<br \/><\/code>            <code><strong>PrintMessage ();<br \/><\/strong>}<\/code><\/p>\n<p>Now if you save the script and run the<br \/>\ngame, the Console will print the value of message and the value of PrintMessage.<\/p>\n<h2 id=\"params\">Part 4: Setting Method Parameters in C#<\/h2>\n<p>What if we want to give PrintMessage<br \/>\nparameters? Delete the number1 and number2 variable lines from the<br \/>\nGameController class. Instead, use the following code to set number1 and<br \/>\nnumber2 to be parameters of PrintMessage.<\/p>\n<p><code>void PrintMessage (int number1, int number2) {<br \/><\/code>             <code><strong>Debug.Log(\"The sum is: \" + (number1 + number2));<br \/><\/strong>}<\/code><\/p>\n<p>This changes PrintMessage to receive 2 integers as input, rather than setting the integers on the developer side.<\/p>\n<h2 id=\"errors\">Part 5: Handling Errors in Unity<\/h2>\n<p>If you save the script and play the game<br \/>\nnow, you will receive the following error message in the Console:<\/p>\n<p><em>Assets\/Scripts\/GameController.cs(12,3):<br \/>\nerror CS1501: No overload for method \u2018PrintMessage\u2019 takes \u20180\u2019 arguments<\/em><\/p>\n<p>This message means that in the<br \/>\nGameController.cs file, at line 12, PrintMessage has no parameters when it<br \/>\nshould. Because we created PrintMessage with parameters, we need to call it<br \/>\nwith the same parameters. Use the following format to pass 20 as the number1<br \/>\nparameter and 22 as the number2 parameter.<\/p>\n<p><code>void Start () {<br \/><\/code>               <code>Debug.Log (message)<br \/><\/code>               <code><strong>PrintMessage (20, 22);<br \/><\/strong>}<\/code><\/p>\n<p>This passing of parameters is like passing<br \/>\nmessage as the parameter of the Log method. <\/p>\n<p>Now if you save the script and play the<br \/>\ngame, the Console will print \u201cLet\u2019s go!\u201d and \u201cThe sum is<br \/>\n42\u201d.<\/p>\n<h2 id=\"message\">Part 6: Building a Message<\/h2>\n<p>Suppose we want to build a message rather than print it. Let\u2019s try this out! Rename PrintMessage \u201cBuildMessage\u201d. Replace <strong>void<\/strong> with string. Our method will now return a value as a string rather than an integer.<\/p>\n<p><code><strong>string <\/strong>PrintMessage (int number1, int number2) {<br \/><\/code>                 <code>Debug.Log (\"The sum is: \" + (number1 + number2));<br \/>}<\/code><\/p>\n<p>Instead of calling the Log method to print the sum, use the keyword <strong>return<\/strong> to return the message. With the return keywod, we can use the method\u2019s return value anytime by calling the method by name. <\/p>\n<p><code>string PrintMessage (int number1, int number2) {<br \/><\/code>                 <code><strong>return<\/strong> (\"The sum is: \" + (number1 + number2));<br \/>}<\/code><\/p>\n<p>In the Start method, replace the<br \/>\nPrintMessage call with a Debug line. Call Log with BuildMessage as a parameter.\n<\/p>\n<p>Give BuildMessage the parameters 1 as the value for the number1 parameter and 2 as the value for the number2 parameter.<\/p>\n<p><code>void Start () {<br \/><\/code>                   <code>Debug.Log (message);<br \/><\/code>                   <code><strong>Debug.Log (BuildMessage (number1, number2));<br \/><\/strong>}<\/code><\/p>\n<p>Calling BuildMessage () will return its<br \/>\nreturn value.<\/p>\n<p>Let\u2019s test our new method! Save the script, and open Unity. Press Play. The Console will print \u201cLet\u2019s go!\u201d and \u201cThe sum is 3\u201d.<\/p>\n<p>Press Stop, and don\u2019t forget to save your project.<\/p>\n<h2>Review<\/h2>\n<p><em>\u2014Team Mammoth from Mammoth Interactive INC. Tutorial by Glauco Pires and Transcribing by Alexandra Kropova<\/em><\/p>\n<h2>More resources on this topic:<\/h2>\n<\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><br \/>\n<br \/>[ad_2]<br \/>\n<br \/><a href=\"http:\/\/www.gamedevelopmentblog.com\/2019\/12\/methods-in-csharp-programming-for-unity-game-development\/\" target=\"_blank\" rel=\"noopener\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] \u201cIt is the method behind the madness that makes it madness.\u2019 \u2015 Marty Rubin Do you want to be a game developer? Learn C# programming fundamentals in Unity with this free tutorial by @MammothCompany! All about methods\u2026.Where would we be without them? Tweet In this tutorial: Setup To follow along with this tutorial, you\u2019ll [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[18],"tags":[],"class_list":["post-855","post","type-post","status-publish","format-standard","hentry","category-gamedev"],"blocksy_meta":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2TFCd-dN","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/comments?post=855"}],"version-history":[{"count":0,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/855\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/media?parent=855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/categories?post=855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/tags?post=855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}