• 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
18
Question by DtBeloBrown · Apr 07, 2010 at 08:05 PM · editorsceneeditor-scriptingscene-loading

Get the currently open scene name or file name

I have an editor script that needs to know which one of my scenes is open in the editor at the moment. Preferably it would give me the name of the file, like "myScene.unity" or "Assets/path/myScene.unity".

Any ideas?

EDIT: My editor script needs to make persistent changes to the scene, therefore the play button will not be pressed while it runs.

Comment
Add comment · Show 5
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 Grimmy · Jul 28, 2010 at 03:40 PM -1
Share

But how do I get rid of the path part of the string so I just have myCurrentScene.unity rather than Assets/path/myCurrentScene.unity??

avatar image Mike 3 Grimmy · Jul 28, 2010 at 04:05 PM 3
Share

System.IO.Path.GetFileName(yourPath) - though this really should have been a seperate question

avatar image Wyern1 · Oct 22, 2015 at 10:50 AM 0
Share

To remove the path part you can use string's .split function, which will return you string[]. The last string on the array will be the scene.unity

avatar image divinegames · Dec 15, 2015 at 09:02 AM 0
Share

Any solution in Unity 5.3? The old solutions seem not working anymore.

avatar image KdRWaylander divinegames · Dec 15, 2015 at 09:06 AM 0
Share

Try using or looking docs about Scene.name

http://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene-name.html

7 Replies

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

Answer by Molix · Apr 22, 2010 at 05:57 PM

EditorApplication.currentScene

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 DtBeloBrown · Apr 22, 2010 at 09:43 PM 0
Share

Correct!!! Thank you. Realizing that EditorApplication exists has solved another problem of mine.

avatar image cregox · Nov 18, 2011 at 01:16 PM 0
Share

In execution time it will bring the scene in which the script was loaded first, not the current loaded level / scene, unlike Application.loadedLevelName, as Elliot already advised.

avatar image syclamoth · Nov 18, 2011 at 01:50 PM 0
Share

That's why you switch to using Application.loadedLevelName at runtime. It's an editor script, anyway- you shouldn't really be using it at runtime.

avatar image ABerlemont · Apr 02, 2015 at 08:31 AM 0
Share

FYI Sometimes when using Application.LoadLevelAdditive() the variable Application.loadedLevelName does not match current opened scene.

avatar image Fattie · Jan 24, 2016 at 07:21 PM 0
Share

win a bounty http://answers.unity3d.com/questions/1132406/editor-script-to-create-bulk-audiosource-bounty.html

avatar image
31

Answer by e.bonneville · Apr 07, 2010 at 08:16 PM

Try (Application.loadedLevelName). You can find the (scanty) page on it here. It gives you the name of the last level loaded, which is probably the current level.

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 DtBeloBrown · Apr 07, 2010 at 08:26 PM 0
Share

So close! That trick only works when the play button has been pressed. Excellent try though, I didn't find that with my searches.

avatar image ocimum DtBeloBrown · Oct 22, 2015 at 01:40 PM 0
Share

Thats not true, you can also use it inside an Editor or EditorWindow. And its just the name of the scene ins$$anonymous$$d the path you will receive with currentScene.

avatar image e.bonneville · Apr 07, 2010 at 08:34 PM 0
Share

Oh well. BTW, the API for Unity is available at this link; http://unity3d.com/support/documentation/ScriptReference/

avatar image DtBeloBrown · Apr 07, 2010 at 08:59 PM 0
Share

I prefer this custom search, which includes the script reference: http://www.google.com/cse/home?cx=002470491425767499270:iugs1ezlsfq

avatar image e.bonneville · Apr 08, 2010 at 02:28 AM 0
Share

O$$anonymous$$, just checking, you know. :)

avatar image
27

Answer by ABerlemont · Jan 20, 2016 at 04:04 PM

Since they added the SceneManager the right way to do that is

http://answers.unity3d.com/questions/1116932/what-is-the-new-scenemanagement-equivalent-for-app.html

 Scene scene = SceneManager.GetActiveScene();
 scene.name; // name of scene
Comment
Add comment · Show 2 · 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 MasterMeyer · Feb 25, 2016 at 10:44 AM 1
Share

they also have EditorScene$$anonymous$$anager.GetActiveScene() for usage in the editor

avatar image alxcancado · Sep 15, 2017 at 06:35 PM 0
Share

Best answer!

avatar image
0

Answer by Dealzu-The-Wikid · Jul 15, 2014 at 10:17 PM

I finally with all the clues after reading for several hours on the forums found the solution to this.. On your first script where you call application.LoadLevel, it is correct this script will be destroyed and not run anymore code... On the player I made another script to determine which zone I was in and used onLevelWasLoaded function... Inside this function I figured out which scene I had just loaded with Application.loadedLevelName.. and THEN and ONLY THEN, despite advice from all over these forums, could I place the player's position for this zone. Here is a quick example.. Make sure your zones have proper spacing in them or this will not work! Also make sure that you don't forget to label the locations you want to teleport to in the inspector :)

 //this code goes on your teleporter
 #pragma strict
 var MyLevel:String;
 var MyPlayer:GameObject;
 var Destination : Transform;
  
 function OnTriggerEnter (other : Collider) 
 {
  if (other.gameObject.tag == "Player") 
      {
           MyPlayer = other.gameObject;
           DontDestroyOnLoad(MyPlayer);
          Application.LoadLevel (MyLevel);     
      }
 }
 
 //this code goes on the player
 #pragma strict
 var portalDropoff : Vector3;
 var portalDropoffCOL  : Vector3;
 
 
 function OnLevelWasLoaded() 
 {
     portalDropoff = gameObject.Find("portalDropoff").transform.position;
     if (Application.loadedLevelName == "Whatever the name of your zone 1 is as a string")
     {
     this.transform.position = portalDropoff;
     }
     if(Application.loadedLevelName == "whatever the name of the second zone is")
     {
     this.transform.position = portalDropoffCOL;    
     }
 }
 

 
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
avatar image
0

Answer by vanderamaral1 · Feb 10, 2016 at 02:42 PM

SceneManager.GetActiveScene().name

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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 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

Related Questions

Cannot close/unload a scene that is open in editor during playmode (using C# code)? 3 Answers

Handle editor playmode exit 0 Answers

Editor Scene? 1 Answer

Handling scene objects 1 Answer

Real time scene changes 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges