Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 14.45 KB | None | 0 0
  1. {
  2.    "questionId": 1109497,
  3.    "questionUrl": "/questions/1109497/unity-53-how-to-load-current-level.html",
  4.    "body": "\n                <p>Hello, before Unity 5.3, I could do </p>\n<pre class=\"prettyprint linenums\"><ol class=\"linenums\"><li class=\"L0\"><code><span class=\"pln\"> </span><span class=\"typ\">Application</span><span class=\"pun\">.</span><span class=\"typ\">LoadLevel</span><span class=\"pun\">(</span><span class=\"typ\">Application</span><span class=\"pun\">.</span><span class=\"pln\">loadedLevel</span><span class=\"pun\">);</span></code></li><li class=\"L1\"><code></code></li></ol></pre>\n<p>But now it&apos;s something weird with SceneManager. I&apos;ve read documentation but nothing. How do I get the current scene and load it (Unity 5.3f4)?</p>\n<p>Thanks!\n</p>\n\n                ",
  5.    "title": "Unity 5.3 How to load current level",
  6.    "score": "14",
  7.    "author": "kevinrocks_786",
  8.    "date": null,
  9.    "comments": [
  10.       {
  11.          "id": 1109504,
  12.          "author": "ArtOfWarfare",
  13.          "date": "2015, Dec 09 at 04:38:50+0000",
  14.          "body": "<p>In my code, I refer to the scene by name. That obviously won&apos;t work if you need to use this script between multiple scenes though. In the answer that you commented on (thus bringing me here), I had suggested what appeared to be the right way to programmatically determine the current scene... Did that not work?\n</p>",
  15.          "children": [
  16.             {
  17.                "id": 1109505,
  18.                "author": "kevinrocks_786",
  19.                "date": "2015, Dec 09 at 04:41:46+0000",
  20.                "body": "<p>I tried what you said. No. I tried this: </p>\n<pre><code> SceneManager.LoadScene(SceneManager.GetActiveScene()) ;\n\n</code></pre>\n<p>Also thanks for coming.</p>",
  21.                "children": []
  22.             }
  23.          ]
  24.       },
  25.       {
  26.          "id": 1126374,
  27.          "author": "jarado9",
  28.          "date": "2016, Jan 13 at 10:27:22+0000",
  29.          "body": "<p>The one I having difficulty figuring out is how to check if loaded scene has a certain name, e.g before 5.3 if (Application.loadedLevelName == MainScene) { do something!! }</p> \n<p>anyone can help with this? </p>",
  30.          "children": [
  31.             {
  32.                "id": 1133922,
  33.                "author": "Scripter05",
  34.                "date": "2016, Jan 27 at 02:36:33+0000",
  35.                "body": "<p>It&apos;s probably too late now, but anyway, here is the code.</p> \n<pre><code> if (SceneManager.GetActiveScene().name == &quot;Name_Of_Your_Scene&quot;)\n         {\n             // Do something...\n         }\n</code></pre>",
  36.                "children": [
  37.                   {
  38.                      "id": 1182078,
  39.                      "author": "jarado9",
  40.                      "date": "2016, May 05 at 23:11:23+0000",
  41.                      "body": "<p>Thanks, will be helpful </p>",
  42.                      "children": []
  43.                   }
  44.                ]
  45.             },
  46.             {
  47.                "id": 1182082,
  48.                "author": "Le-Pampelmuse",
  49.                "date": "2016, May 05 at 23:23:11+0000",
  50.                "body": "<p>I&apos;ve converted your answer to a proper comment. Don&apos;t post an answer if you don&apos;t have a solution to provide.\n</p>",
  51.                "children": []
  52.             }
  53.          ]
  54.       },
  55.       {
  56.          "id": 1144056,
  57.          "author": "ggirgin",
  58.          "date": "2016, Feb 18 at 13:39:52+0000",
  59.          "body": "<p>add also this;</p> \n<p>using UnityEngine.SceneManagement; </p>",
  60.          "children": []
  61.       }
  62.    ],
  63.    "answers": [
  64.       {
  65.          "body": "\n                    <p>someone in the #unity3d irc chat helped me figure out it is this:</p>\n<pre class=\"prettyprint linenums\"><ol class=\"linenums\"><li class=\"L0\"><code><span class=\"pln\">  </span><span class=\"typ\">SceneManager</span><span class=\"pun\">.</span><span class=\"typ\">LoadScene</span><span class=\"pun\">(</span><span class=\"typ\">SceneManager</span><span class=\"pun\">.</span><span class=\"typ\">GetActiveScene</span><span class=\"pun\">().</span><span class=\"pln\">buildIndex</span><span class=\"pun\">);</span></code></li><li class=\"L1\"><code></code></li></ol></pre>\n<p><code>GetActiveScene()</code> returns the current scene object then you can use <code>.name</code> or <code>.buildindex</code>in <code>LoadScene()</code></p>\n<p><a href=\"http://docs.unity3d.com/ScriptReference/SceneManagement.Scene.html\">http://docs.unity3d.com/ScriptReference/SceneManagement.Scene.html</a></p>\n<p>*Edit: Addition from Double_D you need:</p>\n<pre class=\"prettyprint linenums\"><ol class=\"linenums\"><li class=\"L0\"><code><span class=\"pln\">  </span><span class=\"kwd\">using</span><span class=\"pln\"> </span><span class=\"typ\">UnityEngine</span><span class=\"pun\">.</span><span class=\"typ\">SceneManagement</span><span class=\"pun\">;</span></code></li></ol></pre>\n<p>Here&apos;s an example script of mine: <a href=\"https://github.com/sia/Khomaniac/blob/master/Assets/_Scripts/FallDeathRestarter.cs\">https://github.com/sia/Khomaniac/blob/master/Assets/_Scripts/FallDeathRestarter.cs</a></p>\n\n                    ",
  66.          "score": "43",
  67.          "author": "shahan",
  68.          "comments": [
  69.             {
  70.                "id": 1109615,
  71.                "author": "ArtOfWarfare",
  72.                "date": "2015, Dec 09 at 11:44:17+0000",
  73.                "body": "<p>Odd that <code>LoadScene</code> can&apos;t actually take a <code>Scene</code> object.\n</p>",
  74.                "children": [
  75.                   {
  76.                      "id": 1109710,
  77.                      "author": "Bonfire-Boy",
  78.                      "date": "2015, Dec 09 at 15:06:52+0000",
  79.                      "body": "<p>What do you mean by &quot;a Scene object&quot;? \n</p>",
  80.                      "children": [
  81.                         {
  82.                            "id": 1109720,
  83.                            "author": "ArtOfWarfare",
  84.                            "date": "2015, Dec 09 at 15:34:21+0000",
  85.                            "body": "<p>I&apos;m not sure what there is to be confused about... <code>SceneManager.GetActiveScene()</code> returns a <code>Scene</code> object. You have to quite a bit more rep than I do so I&apos;m not sure how you wouldn&apos;t understand that. Perhaps it&apos;s not called an object in C#? I&apos;ve only been using the language for two weeks now, but it seems pretty similar to ever other OO language I know (Java, Python, C++, etc...)\n</p>",
  86.                            "children": [
  87.                               {
  88.                                  "id": 1109726,
  89.                                  "author": "Bonfire-Boy",
  90.                                  "date": "",
  91.                                  "body": "<p>Thank you for the explanation an for the critique :) There&apos;s lots in Unity and few people who are experts in all of it. I&apos;ve not used this SceneManager thing, it&apos;s new. &quot;Scene object&quot; is also a way of referring to objects that are saved in the scene as opposed to being created at runtime (see eg <a href=\"http://docs.unity3d.com/Manual/UNetSceneObjects.html%29.\">http://docs.unity3d.com/Manual/UNetSceneObjects.html).</a>  </p>",
  92.                                  "children": []
  93.                               },
  94.                               {
  95.                                  "id": 1109745,
  96.                                  "author": "Dave-Carlile",
  97.                                  "date": "",
  98.                                  "body": "<p><a rel=\"user\" href=\"/users/478092/bonfire-boy.html\" nodeid=\"478092\">@Bonfire</a>Boy Don&apos;t you know that once you cross the 2k rep barrier you&apos;re required to read the new manual in its entirety the day of release?  /s\n</p>",
  99.                                  "children": []
  100.                               },
  101.                               {
  102.                                  "id": 1109771,
  103.                                  "author": "Owen-Reynolds",
  104.                                  "date": "",
  105.                                  "body": "<p>AoW: Many game coders don&apos;t have any formal ComSci training. They&apos;re bright and clever, but have gaps that would be covered in ComSci227 (why does everyone write 101? That&apos;s always a Careers/Survey class?) Not a dig -- I&apos;m a self-taught modeler and have the same problems there.</p>\n<p>Also, lots of ways to gain Rep here. Don&apos;t take it high/low Rep too seriously.\n</p>",
  106.                                  "children": []
  107.                               }
  108.                            ]
  109.                         }
  110.                      ]
  111.                   },
  112.                   {
  113.                      "id": 1118733,
  114.                      "author": "Nighthawk1029",
  115.                      "date": "2015, Dec 29 at 09:32:04+0000",
  116.                      "body": "<p>This worked for me thanks! but for some strange reason the lighting in my scene changed weirdly. At first the scene is lit really well and then after reset the lighting goes down tramatically and I have no idea why... </p>",
  117.                      "children": []
  118.                   }
  119.                ]
  120.             },
  121.             {
  122.                "id": 1109704,
  123.                "author": "nevaran",
  124.                "date": "2015, Dec 09 at 14:55:45+0000",
  125.                "body": "<p>Oh <strong>wow</strong>, thanks a ton - i didnt though of that! Was actually banging my head on how it was done xD </p>",
  126.                "children": [
  127.                   {
  128.                      "id": 1109759,
  129.                      "author": "shahan",
  130.                      "date": "2015, Dec 09 at 17:17:53+0000",
  131.                      "body": "<p>no problem. I was very confused as well especially since this is such an essential thing to do in my game lol.\n</p>",
  132.                      "children": []
  133.                   }
  134.                ]
  135.             },
  136.             {
  137.                "id": 1111622,
  138.                "author": "Double_D",
  139.                "date": "2015, Dec 13 at 14:35:04+0000",
  140.                "body": "<p>Thanks for the help, but I still spun my n00b wheels for a minute on this one, because you must include the following line at the top of your script in order to use the SceneManager:</p> \n<pre><code> using UnityEngine.SceneManagement;\n</code></pre>",
  141.                "children": [
  142.                   {
  143.                      "id": 1111761,
  144.                      "author": "shahan",
  145.                      "date": "2015, Dec 13 at 20:16:15+0000",
  146.                      "body": "<p>Oh snap good addition. No joke I spun my noob wheels on that for a while too lol.\n</p>",
  147.                      "children": []
  148.                   }
  149.                ]
  150.             },
  151.             {
  152.                "id": 1346356,
  153.                "author": "bunnybean",
  154.                "date": "2017, Apr 28 at 00:13:27+0000",
  155.                "body": "<p>Hi, I was curious about how to reference a level in code. I bolded what I need to fix. I need to be able to have the player move up a level. Thank you! </p> \n<pre><code> void Update(){\n</code></pre> \n<p>if(Input.GetKeyDown(KeyCode.Space) &amp;&amp; count &gt;= winneed <strong>&amp;&amp; Scene.isLoaded(&quot;first thing&quot;</strong>)) {Application.LoadLevel(&quot;second thingy&quot;); }</p> \n<p>if(Input.GetKeyDown(KeyCode.Space) &amp;&amp; count &gt;= winneed &amp;&amp; <strong>Scene.isLoaded(&quot;second thingy&quot;)</strong>) {Application.LoadLevel(&quot;third thing&quot;); } }</p>",
  156.                "children": []
  157.             }
  158.          ],
  159.          "date": null
  160.       },
  161.       {
  162.          "body": "\n                    <p>If &quot;using UnityEngine.SceneManagement;&quot; does not work for you in javascript try:</p> \n<p>import UnityEngine.SceneManagement;</p> \n<p>That worked for me. </p>\n                    ",
  163.          "score": "3",
  164.          "author": "djindepth12",
  165.          "comments": [
  166.             {
  167.                "id": 1199637,
  168.                "author": "KennethUT",
  169.                "date": "2016, Jun 08 at 13:32:31+0000",
  170.                "body": "<p>This helped me for my JavaScript updates. The documentation is incorrect at the bottom of the page under Description where it states:</p> \n<p>Note: In JavaScript add:</p> \n<p>using UnityEngine.SceneManagement;</p> \n<p><a rel=\"nofollow\" href=\"http://docs.unity3d.com/530/Documentation/ScriptReference/SceneManagement.SceneManager.LoadScene.html\">http://docs.unity3d.com/530/Documentation/ScriptReference/SceneManagement.SceneManager.LoadScene.html</a> </p> \n<pre><code> import UnityEngine.SceneManagement;\n\n</code></pre> \n<p>solved the issue for me.</p>",
  171.                "children": []
  172.             }
  173.          ],
  174.          "date": null
  175.       },
  176.       {
  177.          "body": "\n                    <p>This works 100%!</p> \n<pre class=\"prettyprint linenums\"><ol class=\"linenums\"><li class=\"L0\"><code><span class=\"pln\">    </span><span class=\"kwd\">public</span><span class=\"pln\"> </span><span class=\"kwd\">void</span><span class=\"pln\"> </span><span class=\"typ\">Reload</span><span class=\"pun\">()</span></code></li><li class=\"L1\"><code><span class=\"pln\">     </span><span class=\"pun\">{</span></code></li><li class=\"L2\"><code><span class=\"pln\">         </span><span class=\"kwd\">int</span><span class=\"pln\"> scene </span><span class=\"pun\">=</span><span class=\"pln\"> </span><span class=\"typ\">SceneManager</span><span class=\"pun\">.</span><span class=\"typ\">GetActiveScene</span><span class=\"pun\">().</span><span class=\"pln\">buildIndex</span><span class=\"pun\">;</span></code></li><li class=\"L3\"><code><span class=\"pln\">         </span><span class=\"typ\">SceneManager</span><span class=\"pun\">.</span><span class=\"typ\">LoadScene</span><span class=\"pun\">(</span><span class=\"pln\">scene</span><span class=\"pun\">,</span><span class=\"pln\"> </span><span class=\"typ\">LoadSceneMode</span><span class=\"pun\">.</span><span class=\"typ\">Single</span><span class=\"pun\">);</span></code></li><li class=\"L4\"><code><span class=\"pln\">         </span><span class=\"typ\">Time</span><span class=\"pun\">.</span><span class=\"pln\">timeScale </span><span class=\"pun\">=</span><span class=\"pln\"> </span><span class=\"lit\">1</span><span class=\"pun\">;</span></code></li><li class=\"L5\"><code><span class=\"pln\">     </span><span class=\"pun\">}</span></code></li><li class=\"L6\"><code></code></li></ol></pre> \n<p>Don&#xB4;t forget to add &quot; <code>using UnityEngine.SceneManagement;</code> &quot;</p>\n                    ",
  178.          "score": "2",
  179.          "author": "Founasek",
  180.          "comments": [],
  181.          "date": null
  182.       }
  183.    ]
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement