{"id":470,"date":"2025-05-29T14:27:54","date_gmt":"2025-05-29T14:27:54","guid":{"rendered":"https:\/\/ouritsource.com\/blog\/?p=470"},"modified":"2025-05-29T15:23:34","modified_gmt":"2025-05-29T15:23:34","slug":"how-to-add-scores-to-a-game-in-unity-2d","status":"publish","type":"post","link":"https:\/\/ouritsource.com\/blog\/how-to-add-scores-to-a-game-in-unity-2d\/","title":{"rendered":"How to Add Scores to a Game in Unity 2D"},"content":{"rendered":"<body>\n<ol class=\"wp-block-list\">\n<li class=\"has-medium-font-size\">Create a text object by right-clicking in a blank area in the Hierarchy section, then choosing UI &gt; Text, from the pop-up menu.<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"456\" height=\"526\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image.png?resize=456%2C526&#038;ssl=1\" alt=\"\" class=\"wp-image-471\" style=\"width:548px;height:auto\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image.png?w=456&amp;ssl=1 456w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image.png?resize=260%2C300&amp;ssl=1 260w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\"><\/p>\n\n\n\n<p class=\"has-medium-font-size\">2. Create a C# Script (MonoBehaviour) and change name to <strong>ScoreScript.cs.<\/strong> This script will be responsible  for displaying the new score value. See example below:<\/p>\n\n\n\n<p>##############################<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-4ad4a5c29be26d87aab9e26f81271864\">using UnityEngine;<br>using UnityEngine.UI;<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-70027e1fc73daa583451bf5f17fc75d9\">public class ScoreScript : MonoBehaviour<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-d3e05b77449a8517fb906aae616f1a94\">{<br>\u00a0 \u00a0 public static int scoreValue = 0;<br>\u00a0 \u00a0 Text score;<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-b5885181a7d42d1b7c72c4801b2f84ef\">\u00a0 \u00a0 void Start()<br>\u00a0 \u00a0 {<br>\u00a0 \u00a0 \u00a0 \u00a0 score = GetComponent&lt;Text&gt;();<br>\u00a0 \u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-d2bfb3c0026e2d8399c45ee29a38ffc5\">\u00a0 \u00a0 void Update()<br>\u00a0 \u00a0 {<br>\u00a0 \u00a0 \u00a0 \u00a0 score.text = \u201cScore: \u201d + scoreValue;<br>\u00a0 \u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-24bd84a5d027280fbb2d852eed15ce3d\">}<\/p>\n\n\n\n<p>###############################<\/p>\n\n\n\n<p>3. Save the script and drag the new script to your ScoreText Game Object as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"566\" height=\"609\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-1.png?resize=566%2C609&#038;ssl=1\" alt=\"\" class=\"wp-image-472\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-1.png?w=566&amp;ssl=1 566w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-1.png?resize=279%2C300&amp;ssl=1 279w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/figure>\n\n\n\n<p>4. On another script that is calculating the score, you will need to use the onTrigger() function to provide an updated value for score.  In the example script below,  if a Game Object such as a \u201ccoin\u201d is touched by the Player,  the player\u2019s score increases by 10 points.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-53a4a0006cb43055804faa2a83b30a46\">using UnityEngine;<br>public class Coins : MonoBehaviour<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-f4752e04bfca69a40b1d3e5cfdaa50ee\">{<br>\u00a0 \u00a0 private GameObject player;\u00a0 \u00a0 <\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-28019e1b0ef1a308bc04267e69fcf03d\">\u00a0 \u00a0 void Start()<br>\u00a0 \u00a0 {<br>\u00a0 \u00a0 \u00a0 \u00a0 player = GameObject.FindGameObjectWithTag(\u201cPlayer\u201d);<br>\u00a0 \u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-1a4d217b0792ee9d3ae94f9c58d8bb7a\">\u00a0 \u00a0 private void OnTriggerEnter2D(Collider2D collision)<br>\u00a0 \u00a0 {<br>\u00a0 \u00a0 \u00a0 \u00a0 if (collision.tag == \u201cBorder\u201d)<br>   \u00a0 \u00a0 \u00a0 \u00a0 {<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Destroy(this.gameObject);<br>\u00a0 \u00a0 \u00a0 \u00a0 }<br>\u00a0 \u00a0 \u00a0 \u00a0 else if (collision.tag == \u201cPlayer\u201d)<br>\u00a0 \u00a0 \u00a0 \u00a0 {<br>\u00a0 \u00a0        \/\/adds 10 points to  scoreValue in ScoreScript.cs  then destroys the \u201ccoin\u201d from the screen<br>   \u00a0 \u00a0 \u00a0 \u00a0 ScoreScript.scoreValue += 10;<br>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Destroy(this.gameObject);<br>     \u00a0 \u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-24bd84a5d027280fbb2d852eed15ce3d\">}<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-24bd84a5d027280fbb2d852eed15ce3d\">}<\/p>\n\n\n\n<p>5. In the game example below, the player is the Shark and whenever the Shark eats a Tuna, the player\u2019s score increases by 10 points for each Tuna fish it eats.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Score Element in Unity\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/lxKQLR0cHSE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>2. Create a C# Script (MonoBehaviour) and change name to ScoreScript.cs. This script will be responsible for displaying the new score value. See example below: ############################## using UnityEngine;using UnityEngine.UI; public class ScoreScript&#8230;<\/p>\n","protected":false},"author":1,"featured_media":492,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[37],"tags":[],"class_list":["post-470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unity-games","has-thumbnail"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-29-2025-11_22_31-AM.png?fit=1024%2C1024&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":3,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":479,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions\/479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media\/492"}],"wp:attachment":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}