{"id":90,"date":"2020-06-03T04:22:40","date_gmt":"2020-06-03T04:22:40","guid":{"rendered":"https:\/\/lucyestela.com\/dev\/?p=90"},"modified":"2021-03-02T03:01:08","modified_gmt":"2021-03-02T03:01:08","slug":"destroy","status":"publish","type":"post","link":"https:\/\/lucyestela.com\/dev\/unity\/destroy\/","title":{"rendered":"Destroy &#8211; Unity"},"content":{"rendered":"<div class=\"boldgrid-section\">\n<div class=\"container\">\n<div class=\"row\">\n<div class=\"col-md-12 col-xs-12 col-sm-12\">\n<pre><span style=\"font-family: Menlo;\"> <span style=\"color: #009695;\">public<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">static<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">void<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">Destroy<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">Object<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">obj<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #009695;\">float<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">t<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #222222;\">=<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #db7100;\">0.0F<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<p class=\"\">The Unity docs have a good description of the Destroy method <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Object.Destroy.html\">here. <\/a>But in case you just want some quick code here it is.<\/p>\n<p class=\"\">To destroy a gameobject from within itself, just call&nbsp;<\/p>\n<pre><span style=\"font-family: Menlo;\"> <span style=\"color: #222222;\">Destroy<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">gameObject<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<p class=\"\">To destroy a gameobject from within after 5 seconds<\/p>\n<pre><span style=\"font-family: Menlo;\"> <span style=\"color: #222222;\">Destroy<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">gameObject<\/span><span style=\"color: #222222;\">,<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: #db7100;\">5<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<p>To destroy a script instance from a gameobject<\/p>\n<pre><span style=\"font-family: Menlo;\"> <span style=\"color: #222222;\">Destroy<\/span><span style=\"color: #222222;\">(t<\/span><span style=\"color: #222222;\">his<\/span><span style=\"color: #222222;\">);<\/span><\/span><\/pre>\n<p class=\"\">To destroy a component on a gameobject, in this example a rigidbody<\/p>\n<pre><span style=\"font-family: Menlo;\"> <span style=\"color: #222222;\">Destroy<\/span><span style=\"color: #222222;\">(<\/span><span style=\"color: #222222;\">GetComponent<\/span><span style=\"color: #222222;\">&lt;<\/span><span style=\"color: #222222;\">Rigidbody<\/span><span style=\"color: #222222;\">&gt;());<\/span><\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>public&nbsp;static&nbsp;void&nbsp;Destroy(Object&nbsp;obj,&nbsp;float&nbsp;t&nbsp;=&nbsp;0.0F); The Unity docs have a good description of the Destroy method here. But in case you just want some quick code here it is. To destroy a gameobject from within itself, just call&nbsp; Destroy(gameObject); To destroy a gameobject from within after 5 seconds Destroy(gameObject,&nbsp;5); To destroy a script instance from a gameobject Destroy(this); To &#8230; <a title=\"Destroy &#8211; Unity\" class=\"read-more\" href=\"https:\/\/lucyestela.com\/dev\/unity\/destroy\/\" aria-label=\"More on Destroy &#8211; Unity\">Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"bgseo_title":"","bgseo_description":"","bgseo_robots_index":"index","bgseo_robots_follow":"follow","footnotes":""},"categories":[58],"tags":[24,4],"class_list":["post-90","post","type-post","status-publish","format-standard","hentry","category-unity","tag-destroy","tag-unity"],"_links":{"self":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/90","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/comments?post=90"}],"version-history":[{"count":3,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/90\/revisions"}],"predecessor-version":[{"id":241,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/posts\/90\/revisions\/241"}],"wp:attachment":[{"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/media?parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/categories?post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lucyestela.com\/dev\/wp-json\/wp\/v2\/tags?post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}