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.

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.


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

Scene scene = SceneManager.GetActiveScene();; // name of scene

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 :slight_smile:

//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;
         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;	


SceneManager.GetActiveScene().name holds the name

using UnityEngine.SceneManagement;

Bear in mind that if you put this right after a call to:


it will not have time to put the new scene name.

This is post helped (MasterMeyer) me find this. This is what I had to do to get this to work in the editor

string sceneName = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().name;

Worked like a charm!