- Home /
On Mouse Down Load Previous
I wanted to use function OnMouseDown(){} to load the last level that was open, and I cant find anywhere that shows how to do that? Can someone help? Thanks in advance... EDIT:
I have this script for loading last played level on awake, but i cant figure out the script to attach to a box collider that I want to click to load the last level:
//LevelLoadManager.js
private var currentLevel:int = -1;
function Awake():void
{
//retrieve and load last level played.
DontDestroyOnLoad(gameObject);
currentLevel = PlayerPrefs.GetInt("LastLevelLoaded");
Application.LoadLevel(currentLevel);
}
function Update():void
{
//save the current level if it seems to have changed.
if(Application.loadedLevel != currentLevel)
{
currentLevel = Application.loadedLevel;
PlayerPrefs.SetInt("LastLevelLoaded",currentLevel);
} }
Answer by Seth-Bergman · Aug 12, 2012 at 06:37 PM
instead of Awake, make it a unique function:
function LoadLastLevel()
{
//retrieve and load last level played.
DontDestroyOnLoad(gameObject);
currentLevel = PlayerPrefs.GetInt("LastLevelLoaded");
Application.LoadLevel(currentLevel);
}
then on the cube:
var gameLoader : LevelLoadManager;
function Start(){
gameLoader = GameObject.Find("LoaderObjectName").GetComponent(LevelLoadManager);
}
function OnMouseDown(){
gameLoader.LoadLastLevel();
}
of course you would still keep the Update as in your example as well, just keep it all the same but change Awake to something else, so you can call it..
it's the same as you have now, only this way you call it from On$$anonymous$$ouseDown
Answer by Sundar · Aug 12, 2012 at 05:14 PM
use PlayerPrefs to store current level as previouslevel when you go to next level.
Then call previouslevel OnMouseDown(){}
Your answer