{"id":506,"date":"2025-05-29T17:19:28","date_gmt":"2025-05-29T17:19:28","guid":{"rendered":"https:\/\/ouritsource.com\/blog\/?p=506"},"modified":"2025-05-29T17:23:34","modified_gmt":"2025-05-29T17:23:34","slug":"spawning-a-gameobject-in-unity-2d","status":"publish","type":"post","link":"https:\/\/ouritsource.com\/blog\/spawning-a-gameobject-in-unity-2d\/","title":{"rendered":"Spawning a GameObject in Unity 2D"},"content":{"rendered":"<body>\n<p class=\"has-medium-font-size\">In the following example, we will be spawning a sprite image of a turtle into the game.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-medium-font-size\">Create an Empty GameObject and name it <strong>TurtleSprite<\/strong>. This GameObject will be the object being spawned in the game.<\/li>\n\n\n\n<li class=\"has-medium-font-size\">Drag and drop a PNG or GIF  of a turtle image to the Game and make it a child of the GameObject, TurtleSprite.<\/li>\n\n\n\n<li class=\"has-medium-font-size\">Create a New C# Script (MonoBehaviour) and name it <strong>TurtleSpriteScript.cs.<\/strong><\/li>\n\n\n\n<li class=\"has-medium-font-size\">Open script and add code for the Turtle GameObject such as having the object get removed\/destroyed if it hits the game\u2019s borders or to add points to a player\u2019s score. For example, the code below will add 200 points to the player\u2019s score and then disappear if the player  touches it. <br><\/li>\n<\/ol>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-706f833c6f54848ad3cd0a0c2426b2d3\">################################<br>using System.Collections;<br>using System.Collections.Generic;<br>using UnityEngine;<br><br>public class TurtleSpriteScript : MonoBehaviour<br>{<br>\u00a0\u00a0\u00a0 private void OnTriggerEnter2D(Collider2D collision)<br>\u00a0\u00a0\u00a0 {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (collision.tag == \u201cBorder\u201d)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Destroy(this.gameObject);<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else if (collision.tag == \u201cPlayer\u201d)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ScoreScript.scoreValue += 200;<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Destroy(this.gameObject);<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br>\u00a0\u00a0\u00a0 }<br>}<br>################################<\/p>\n\n\n\n<p class=\"has-medium-font-size\">5. Save the TurtleSpriteScript.cs file and attach to the TurtleSprite Game Object.<br>6. Add <strong>RigidBody 2D and Box Collider 2D <\/strong>to the GameObject as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"584\" height=\"636\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-7.png?resize=584%2C636&#038;ssl=1\" alt=\"\" class=\"wp-image-507\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-7.png?w=584&amp;ssl=1 584w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-7.png?resize=275%2C300&amp;ssl=1 275w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"587\" height=\"654\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-8.png?resize=587%2C654&#038;ssl=1\" alt=\"\" class=\"wp-image-508\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-8.png?w=587&amp;ssl=1 587w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-8.png?resize=269%2C300&amp;ssl=1 269w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\">7. Click <em>Edit Collider<\/em> in Box Collider 2D Component and make adjustments so that the box collider covers entire character.<br>8. Set <em>Gravity <\/em>on RigidBody 2D to 0 so that it does not fall to the bottom.<br>9. Drag the entire TurtleSprint GameObject to your Prefab folder.<br>10. Delete the TurtleSprint GameObject from the game (not the Prefab folder).<\/p>\n\n\n\n<p class=\"has-medium-font-size\">11. Create an empty GameObject and name it <strong>TurtleSpawnPoint.<\/strong><br>12. Create a New C# Script and name it <strong>SpawnTurtle.cs<\/strong>.<br>13. Modify script to provide information on when the Turtle sprites should spawn in the game. For example ,the code below adds 6 attributes we can specify for the spawning action:<\/p>\n\n\n\n<p class=\"has-medium-font-size\">maxX = X Coordinate of the left-side of the game<br>minX = X Coordinate of the right-side of the game<br>maxY = Y Coordinate of the top of the game<br>minY = Y Coordinate of the bottom of the game<br>timeBetweenSpawn = how many seconds when the last turtle was spawned<br>spawnTime = how many seconds to elapse before another turtle is spawned<br><\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-917797be776ae64ff9876ad8fec9d480\">#############<br>using System.Collections;<br>using System.Collections.Generic;<br>using UnityEngine;<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-16aac62f9141e6dd0fd449d67d0c1fac\">public class SpawnTurtle : MonoBehaviour<br>{<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-ef011767ad79a49f28d88f77c6343033\">\u00a0\u00a0\u00a0 public GameObject TurtleSprints;<br>\u00a0\u00a0\u00a0 public float maxX;<br>\u00a0\u00a0\u00a0 public float minX;<br>\u00a0\u00a0\u00a0 public float maxY;<br>\u00a0\u00a0\u00a0 public float minY;<br>\u00a0\u00a0\u00a0 public float timeBetweenSpawn;<br>\u00a0\u00a0\u00a0 private float spawnTime;<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-8fb4b3555cde49028450dfc24873093c\">\u00a0\u00a0\u00a0 \/\/ Update is called once per frame<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-829ba50b8e5d61d7bc457589565819ee\">\u00a0\u00a0\u00a0 void Update()<br>\u00a0\u00a0\u00a0 {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (Time.time &gt; spawnTime)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Spawn();<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 spawnTime = Time.time + timeBetweenSpawn;<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-3b13cceb069fa1baa04e71db03a99b36\">\u00a0\u00a0\u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-2b253cb6c5948fadcc8713b59d78c746\">\u00a0\u00a0\u00a0 void Spawn()<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-1b9ffbbd11ba2bc5425661ed20fc73b2\">\u00a0\u00a0\u00a0 {<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-467828db85e72e5509ea859a027b4f43\">\u00a0\u00a0\u00a0\u00a0 float randomX = Random.Range(minX, maxX);<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 float randomY = Random.Range(minY, maxY);<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-39ac670d87d21b778b44bf0dda616d05\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Instantiate(TurtleSprints, transform.position + new Vector3(randomX, randomY, 0), transform.rotation);<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-3b13cceb069fa1baa04e71db03a99b36\">\u00a0\u00a0\u00a0 }<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-7a1b4fa5b617e2faf32b925f858eadc2\">}<br>#############<\/p>\n\n\n\n<p class=\"has-medium-font-size\">14. Save script and attach it to the TurtleSpawnPoint GameObject.<br>15. Drag the TurtleSprite GameObject created earlier and add to the <em>Sprint<\/em> attribute.<br>16. Fill in the values for the attributes specified in the TurtleSprint.cs script that is now showing in the Script component of game as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"298\" src=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-9.png?resize=640%2C298&#038;ssl=1\" alt=\"\" class=\"wp-image-509\" loading=\"lazy\" srcset=\"https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-9.png?w=975&amp;ssl=1 975w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-9.png?resize=300%2C140&amp;ssl=1 300w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-9.png?resize=768%2C358&amp;ssl=1 768w, https:\/\/i0.wp.com\/ouritsource.com\/blog\/wp-content\/uploads\/2025\/05\/image-9.png?resize=900%2C419&amp;ssl=1 900w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p class=\"has-medium-font-size\">17. Once information is filled, run the game.<br>18. Video below shows an example of Turtles spawning into the game using this method.<\/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=\"Turtle Sprint Spawning Into Game\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/xyiA5ztqKpQ?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<\/body>","protected":false},"excerpt":{"rendered":"<p>In the following example, we will be spawning a sprite image of a turtle into the game. ################################using System.Collections;using System.Collections.Generic;using UnityEngine; public class TurtleSpriteScript : MonoBehaviour{\u00a0\u00a0\u00a0 private void OnTriggerEnter2D(Collider2D collision)\u00a0\u00a0\u00a0 {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if&#8230;<\/p>\n","protected":false},"author":1,"featured_media":512,"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-506","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-01_21_43-PM.png?fit=1024%2C1024&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/506","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=506"}],"version-history":[{"count":3,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/506\/revisions"}],"predecessor-version":[{"id":514,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/posts\/506\/revisions\/514"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media\/512"}],"wp:attachment":[{"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/media?parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/categories?post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ouritsource.com\/blog\/wp-json\/wp\/v2\/tags?post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}