Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I'm confused, Application.loadedLevelName not working

Discussion in 'Scripting' started by graviton, Dec 7, 2015.

  1. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Can someone tell me what's wrong with my code

    I'm not getting any errors, but it's also not doing anything

    and yes I've added the level I'm checking to Build Setting and yes it's ticked

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class loadCheckTest : MonoBehaviour
    5. {
    6.    
    7.     public string Level = "timeLeftTest01";
    8.  
    9.     // Use this for initialization
    10.     void Awake ()
    11.     {
    12.         if(Application.loadedLevelName == Level)
    13.         {
    14.             Debug.Log("onlevel");
    15.         }
    16.     }
    17. }
    18.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    1) What value does Application.loadedLevelName have?

    2) Have you made sure that this level is in the scene list in build settings?

    3) According to the docs: The name of the level that was last loaded. Is this code running in the initial scene (e.g. when you hit Play), or in one that's loaded after that? Application.loadedLevelName might be blank until a scene is actually loaded.
     
    graviton likes this.
  3. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    @StarManta

    I figured it out, it's this line
    Code (csharp):
    1. public string Level = "timeLeftTest01";
    The name I wrote in the string is the name of a script next to the actual scene in my assets folder, whose name I wanted to use for my string

    I must have gone through this code a dozen times and didn't catch it till I closely read your questions and analysed my code above, again

    Thanks, your questions gave my brain a jumpstart

    I think I'm a little overworked or something, here is to clarity