Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by _Adriaan · Feb 24, 2016 at 02:10 PM · scenescene-loadingcheck

How to determine whether a Scene with 'name' exists (with the new SceneManager)?

How can I determine whether a scene exists and can be loaded or not? It seems that SceneManager.GetSceneByName(name) only returns scenes that are loaded...

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gfaraj · Sep 25, 2016 at 05:22 PM 0
Share

Can someone respond to this?

avatar image _Adriaan gfaraj · Sep 25, 2016 at 06:00 PM 0
Share

(see my answer below - I wrote a custom thing for this that I'm using!)

5 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

Answer by JustHonour · Sep 13, 2017 at 12:53 PM

This has worked for me. Although I'm not sure if it's ideal.

     if (Application.CanStreamedLevelBeLoaded("sceneName")
     {
         SceneManager.LoadScene("sceneName");
     }
     else
     {
         //Go to a different scene?
     }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image auzzyWiz · Oct 19, 2017 at 03:15 AM 0
Share

This worked for me. Thank you!

avatar image mckeithen · Nov 17, 2017 at 12:50 PM 0
Share

works like a charm! thanx ;)

avatar image Ziplock9000 · Dec 12, 2020 at 07:00 AM 0
Share

Do not use this, it's a terrible answer

avatar image DearUnityPleaseAddSerializableDictionaries · Oct 23, 2021 at 10:59 PM 0
Share

Ziplock9000 - why do you say "Do not use this, it's a terrible answer"? I haven't come across any problems using this answer myself.

avatar image
6

Answer by HadynTheHuman · Aug 16, 2017 at 07:10 AM

You can use SceneUtility.GetScenePathByBuildIndex to get a list of valid scene names:

 List<string> scenesInBuild = new List<string>();
 for (int i = 1; i < SceneManager.sceneCountInBuildSettings; i++)
 {
     string scenePath = SceneUtility.GetScenePathByBuildIndex(i);
     int lastSlash = scenePath.LastIndexOf("/");
     scenesInBuild.Add(scenePath.Substring(lastSlash + 1, scenePath.LastIndexOf(".") - lastSlash - 1));
 }

Then check if a scene exists with:

 if(scenesInBuild.Contains(sceneName))
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _Adriaan · Aug 16, 2017 at 08:28 AM 0
Share

as of Unity 5.6, this is the appropriate answer!

avatar image battxbox · Aug 25, 2017 at 10:31 AM 0
Share

Why can't we just use "System.IO.Path.GetFileNameWithoutExtension(scenePath))" ins$$anonymous$$d? I tried it and seems working well.

avatar image Erothez · Sep 03, 2017 at 01:45 AM 1
Share

Couldn't you just use?

if(SceneUtility.GetBuildIndexByScenePath(sceneName) >= 0)

(ps. Thank you didn't know about this before)

avatar image Vinicius_Bil Erothez · Feb 21, 2018 at 08:42 PM 0
Share

Dude, you do not know, but you saved my life :)

avatar image Zamahra · Jul 12, 2018 at 12:26 PM 0
Share

Scene indexes start with 0, your list wouldn't add the start scene to scenesInBuild. But nethertheless, thanks! Your answer helped me.

avatar image
0

Answer by SarfaraazAlladin · Sep 25, 2016 at 05:54 PM

The short answer is that you can't.

There are some ways to work around the limitation though. Checkout this post:

http://answers.unity3d.com/questions/1115796/scenemanagergetallscenes-only-returns-the-current.html

It's a shame that there isn't an easy way to get access to the build settings yet. Until Unity addresses it, I hope that post sets you in the right direction.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gfaraj · Sep 25, 2016 at 05:56 PM 0
Share

Unfortunately, it seems like EditorBuildSettings has been deprecated, so I don't think this solution would work today. Thanks anyway. I agree, there should be a very simple way to do this.

avatar image
-1

Answer by FussenKuh · Dec 01, 2016 at 10:08 AM

UPDATE - Scratch everything I've said below. While this should work, the OP is correct, it doesn't... at least in 5.4.3f1 and 5.5.0f3. The quick test I ran was flawed which lead to my misunderstanding.

I'm a bit late to this party and, perhaps I'm misinterpreting the question, but, the following seems to work just fine for me in Unity 5.4.3f1:

 public void LoadScene(string argScene)
 {
     if (UnityEngine.SceneManagement.SceneManager.GetSceneByName(argScene).IsValid())
     {
          < Load My Scene >
     }
     else
     {
         Debug.LogWarning("Load Scene Failed. " + argScene + " not found.");
     }
 }

If I attempt to load a non-existent scene, the .IsValid() function returns FALSE and I dump into my warning message block.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gfaraj · Dec 01, 2016 at 02:40 PM 1
Share

This is the first thing I tried and no, it doesn't work. GetSceneByName will only return scenes that have already been loaded in the first place (which kind of defeats the whole purpose of IsValid, really).

avatar image FussenKuh gfaraj · Dec 01, 2016 at 04:00 PM 0
Share

$$anonymous$$aybe I'm still missing something... Are you using Unity 5.4.3f1 or an older version? I can't vouch for earlier versions, but, after downloading 5.4.3f1 earlier in the week, I wrote that function last night for my current project and it appears to give me the exact behavior I expect.

For example: I have a scene called "Loading." If I call LoadScene("Loading"), I successfully enter the block of "Load $$anonymous$$y Scene" code. On the other hand, if I call my function with an arbitrary string that does not map to a scene in my game, say LoadScene("$$anonymous$$yAwesomeSceneThatDoesNotExist"), I hit the 'else' block and receive the warning message.

If you're running 5.4.3f1 and are seeing different behavior, I'm going to label myself very confused.

avatar image FussenKuh gfaraj · Dec 02, 2016 at 12:40 AM 0
Share

Scratch that...

After poking around a bit more I now understand what the two of you are saying. I'm seeing the same (non) functionality the two of you are reporting.

I'm going to chalk my confusion up to being up too late and failing at reading comprehension coupled with a mis-configuration of the test on my part.

So, yes, GetSceneByName does, indeed, seem completely useless.

avatar image Ziplock9000 · Apr 25, 2017 at 10:11 PM 0
Share

I can confirm this DOES NOT work. 5.6.f3

avatar image fariazz Ziplock9000 · May 24, 2017 at 02:48 AM 0
Share

It also doesn't work on 5.6.1f1.. :(

avatar image
0

Answer by MajeureX · Jul 17, 2020 at 11:00 PM

I've created a method that uses the SceneUtility.GetBuildIndexByScenePath method to check that a scene exists in the build path:

 bool IsSceneLoadableFromPath(string scenePath)
 {
     if (String.IsNullOrWhiteSpace(scenePath))
         return false;
     if (SceneUtility.GetBuildIndexByScenePath(scenePath) >= 0)
         return true;
 
     // Allow some extra variations on scenePath since SceneManager.LoadSceneAsync is
     // more relaxed about scene path
     if (SceneUtility.GetBuildIndexByScenePath(scenePath + ".unity") >= 0)
         return true;
     if (SceneUtility.GetBuildIndexByScenePath("Assets/" + scenePath) >= 0)
         return true;
     if (SceneUtility.GetBuildIndexByScenePath("Assets/" + scenePath + ".unity") >= 0)
         return true;
     return false;
 }

You'll notice that the method makes multiple attempts to validate the given path before finally returning the result false. I've implemented it like this as SceneUtility.GetBuildIndexByScenePath method is more strict/precise in the path it expects than the SceneManager.LoadSceneAsync: the latter method lets you omit 'Assets' directory and '.unity' suffix. These additional checks accomodate these differences.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Classic Resident Evil-style room loading/level streaming? 4 Answers

,Enums not being passed between scripts properly 1 Answer

How do I reset part of a scene? 1 Answer

how to save the progress on scene1 when it goes to scene2, so when it goes back to scene1 its still the same saved scene 2 Answers

Changing scenes while preserving background 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges