0
\$\begingroup\$

I'm making a simple 2D game for Android using the Unity3D game engine. I created all the levels and everything but I'm stuck at making the game over/retry menu. So far I've been using new scenes as a game over menu. I used this simple script:

#pragma strict

var level = Application.LoadLevel;

function OnCollisionEnter(Collision : Collision)
{
    if(Collision.collider.tag == "Player")
    {
        Application.LoadLevel("GameOver");
    }
}

And this as a 'menu':

#pragma strict

var myGUISkin : GUISkin;

var btnTexture : Texture;

function OnGUI() {
GUI.skin = myGUISkin;


if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
    Application.LoadLevel("Easy1");

if (GUI.Button(Rect(Screen.width/2-90,Screen.height/2+100,170,40),"Main Menu"))
    Application.LoadLevel("MainMenu");
}

The problem stands at the part where I have to create over 200 game over scenes, obstacles (the objects that kill the player) and recreate the same script over 200 times for each level. Is there any other way to make this faster and less painful?

\$\endgroup\$
  • 1
    \$\begingroup\$ Why do you have to recreate it 200 times? \$\endgroup\$ – LVBen May 30 '14 at 15:13
  • \$\begingroup\$ I have over 200 levels,I need to copy-paste it then edit each one of them to make it so it opens a specific level. \$\endgroup\$ – Ssiro May 30 '14 at 15:51
  • \$\begingroup\$ As seen on Stackoverflow: stackoverflow.com/questions/23957308/… \$\endgroup\$ – Nestor Ledon May 30 '14 at 16:03
1
\$\begingroup\$

Another option would be to make something like a singleton LevelManager MonoBehavior and add it to an object named "Level Manager". Use the DontDestroyOnLoad function to make the object persist even when you load another level.

class LevelManager : MonoBehavior
{
    static LevelManager _instance;
    public string currentLevelName;
    public string killedBy;

    function Awake ()
    {
       if (_instance == null)
       {
          _instance = this;
           DontDestroyOnLoad(gameObject);
       }
       else
       {
          Destroy(gameObject); // Make sure we never have more than 1 Level Manager.
       }
    }

    LevelManager Instance
    {
       get
       {
          if (_instance == null)
          {
             GameObject levelManagerObject = GameObject.Find("Level Manager");
             _instance = levelManagerObject.GetComponent<LevelManager>();
          }
          return _instance
       }
    }
}

Then, from the main menu class, you can always access the Level Manager like so:

Debug.Log("Killed by " + LevelManager.Instance.killedBy);

or

LevelManager.Instance.currentLevelName = Application.loadedLevelName;
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

There are several different solutions, but I would recommend using PlayerPrefs. This has the extra benefit of persisting even when the application is closed and then re-opened.

In your Awake() function of your Main Menu class, you can get the current level and store it in a static string of your Main Menu class. If it is the player's 1st time, use the name for level 1.

Something like this:

static string currentLevelName;
void Awake()
{
    currentLevelName = PlayerPrefs.GetString("CurrentLevel");
    if (currentLevelName == defaultValue)
    {
       currentLevelName = "Level1"
    }
}

Then, modify your button to do this instead:

if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
    Application.LoadLevel(currentLevelName);

Whenever the player advances to the next level, set the string in PlayerPrefs to the new level name:

PlayerPrefs.SetString("CurrentLevel", Application.loadedLevelName);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Unity has a simple 1-hour video tutorial on making a 2d Infinite runner game. There is a small part in the video in which they use PlayerPrefs to pass a score to a Main Menu scene. \$\endgroup\$ – LVBen May 30 '14 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.