title: get_tree().get_current_scene() is returning null in godot/gdscript
body: <p>I was trying to get the name of the current scene by using <code>get_tree().get_current_scene().get_name()</code> but i'm getting the error:</p>
  
  <blockquote>
    <p>Attempt to call function 'get_name' in base 'null instance' on a null
    instance.</p>
  </blockquote>
  
  <p>I found out that <code>get_current_scene()</code> was giving me <code>null</code>. This is after a change scene where the function was working as intended.
  i.e.
  <code>get_tree().get_current_scene().get_name()</code> in <strong>menu.gd</strong> returns <strong><em>menu</em></strong>
  <code>get_tree().get_current_scene().get_name()</code> in <strong>game.gd</strong> returns the <strong><em>error</em></strong> because <code>get_tree().get_current_scene()</code> is <code>null</code></p>
  
  <p><strong>levelManager.gd</strong> (autoloaded singleton) [full script]</p>
  
  <pre><code>extends Node
  
  const SCENE_PATH = "res://scenes/"
  
  func change_scene(scene_name):
      deffer_call("_deffered_change_scene", scene_name)
  
  func _deffered_change_scene(scene_name):
      var path = SCENE_PATH + scene_name + ".tscn"
      var root = get_tree().get_root()
      var current = root.get_child(root.get_child_count() - 1)
      current.free()
      var scene_resource = ResourceLoader.load(path)
      var new_scene = scene_resource.instance()
      get_tree().get_root().add_child(new_scene)
      get_tree().set_current_scene(new_scene)
  
  func _process(_delta):
      if Input.is_action_pressed("close"):
          get_tree().quit()
  
  func deffer_call(func_name, func_param):
      call_deferred(func_name, func_param)
  </code></pre>
  
  <p>--[<em>Shortened scripts:</em>]--</p>
  
  <p><strong>menu.gd</strong></p>
  
  <pre><code>extends Node
  
  func _ready():
      var scene_name = get_tree().get_current_scene().get_name()
      print(scene_name) # returns 'menu'
  
  # on full version of the script, mechanism that called _change_scene here
  
  func _change_scene():
      levelManager.change_scene("game")
  </code></pre>
  
  <p><strong>game.gd</strong></p>
  
  <pre><code>extends Node
  
  #game variables declared here
  
  func _ready():
      var scene_name = get_tree().get_current_scene().get_name()
      print(scene_name) #returns an error due to get_tree().get_current_scene() being null
  
  # rest of the game logic code here
  </code></pre>
  
  <p>I want to know why this is the case. Is there something wrong with the way i changed scenes from <strong>levelManager.gd</strong>?</p>
  
  <p>I resolved this issue by:
  setting <code>var current_scene_name</code> in <strong>levelManager.gd</strong>
  -- both in <strong>menu.gd</strong> and <strong>game.gd</strong> --
  re-setting <code>current_scene_name</code> by calling <code>self.get_name()</code></p>
  
  <p>i.e.
  <strong>levelManager.gd</strong></p>
  
  <pre><code>var current_scene_name
  </code></pre>
  
  <p>--<strong>menu.gd</strong> and <strong>game.gd</strong> --</p>
  
  <pre><code>var scene_name = self.get_name()
  levelManager.current_scene_name(scene_name)
  </code></pre>
  
  <p>^ This is obviously a workaround. It works but i still want to know why i'm getting different outputs after calling <code>get_tree()</code> while they practically are in the same remote location in the node.</p>
  
  <p><em>Screenshot</em>
  <img src="https://i.stack.imgur.com/5GUbO.jpg" alt="treeScreenshotBeforeAndAfterChangingScenes"></p>