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

if level loaded ?

Discussion in 'Scripting' started by Quast, Aug 25, 2015.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Hi
    I know that if i want to load a level write this:
    Code (CSharp):
    1. Application.LoadLevel("Scene");
    what i want is. if (level2 load it or opened)
    {
    ammo = 10;
    healthy = 20;
    player.speed = 50;
    }
    without press on a button to do this.
    so that i can make some difference in each level.
     
  2. Spoink

    Spoink

    Joined:
    May 9, 2011
    Posts:
    33
    Sounds like you are looking for a container to hold data.
    I usually have a static settings class to carry data between scenes.

    public static class Settings
    {
    public static int Ammo = 10;
    public static int Health = 20;
    }

    then in your method you can read and write to this class

    void RandomMethod()
    {
    Settings.Ammo = 5;
    Debug.Log("Health " + Settings.Health);
    }
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    OnLevelWasLoaded. It's like Update. But for loading levels.
     
    Timelog likes this.
  4. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    If I've understood well, you may need something like this :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class YourScriptName : MonoBehaviour {
    5.  
    6.     void Update () {
    7.         if (Application.loadedLevelName == "Level2")
    8.         {
    9.             // What you want to do.
    10.         }
    11.     }
    12. }
    13.  
     
    Quast likes this.
  5. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    thank you guys.
    loadedLevelName is exactly what i want.
    Thank you Simonxzx.
     
    Simonxzx likes this.
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Kiwasi likes this.