{"id":1227,"date":"2015-02-14T10:13:15","date_gmt":"2015-02-14T10:13:15","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=1227"},"modified":"2015-08-10T06:52:05","modified_gmt":"2015-08-10T06:52:05","slug":"sharing-data-between-scenes-in-unity3d","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/unity3d\/sharing-data-between-scenes-in-unity3d\/","title":{"rendered":"Sharing data between scenes in Unity3D"},"content":{"rendered":"<p>Here&#8217;s I&#8217;m going to show you steps on how to share your data between scenes in Unity3D<\/p>\n<ol>\n<li>In the starting scene make a new empty object and name it <strong>GameData<\/strong>.<\/li>\n<li>Add a script named <strong>GameController<\/strong> with the following content:\n<pre class=\"lang:default decode:true\">#pragma strict\r\n\r\nprivate var data : Array;\r\n\r\nfunction Awake () {\r\n\tDontDestroyOnLoad (this);\r\n}\r\n\r\nfunction Start () {\r\n\tdata = new Array();\r\n\t\r\n\tGetNewResults();\r\n}\r\n\r\nfunction GetNewResults(){\r\n\tvar www : WWW = new WWW (\"http:\/\/localhost\/check\");\r\n\t\r\n\tyield www;\/\/ Wait for download to complete\r\n\t\r\n\tvar dataJson = JSON.Parse(www.data);\r\n\tvar novi = dataJson[\"data\"].Value;\r\n\tdata = dataJson[\"someMoreData\"];\r\n\t\r\n\tif (novi == \"true\"){\t\r\n\t\tApplication.LoadLevel(Application.loadedLevel + 1);\r\n\t}\r\n\telse{\r\n\t\tDebug.Log(\"false\");\r\n\t}\r\n}\r\n\r\npublic function getData(){\r\n\treturn data;\r\n}<\/pre>\n<p>The most important part is <strong>DontDestroyOnLoad(this)<\/strong> which does not destroy this object once a new scene is loaded.<\/li>\n<li>Then, in another scene, in some script do the following to fetch the data:\n<pre class=\"lang:default decode:true  \">var data = GameObject.Find(\"GameData\").GetComponent(GameController).getData();<\/pre>\n<p>&nbsp;<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s I&#8217;m going to show you steps on how to share your data between scenes in Unity3D In the starting scene make a new empty object and name&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1228,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-1227","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-unity3d"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=1227"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1227\/revisions"}],"predecessor-version":[{"id":2023,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/1227\/revisions\/2023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1228"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=1227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=1227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=1227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}