So, here’s what happens… I start at my main menu, click play and load into the first level. From the pause menu if I click Save which uses this block of code:
playerMenu.js if(GUILayout.Button("Save Game")){ PlayerPrefs.SetString("lastLevel", Application.loadedLevelName); }
and then quit and load, which uses this block of code
//mainMenu.js if(GUILayout.Button ("Load Last Level")){ levelLoad = PlayerPrefs.GetString("lastLevel"); Application.LoadLevel(levelLoad); }
When the level is reloaded, the first person controller becomes unresponsive to keyboard input while the mouse is still controllable…
The same thing happens if I quit the game and go back to the main menu and then click either play (My play button is a simple line of “Application.LoadLevel”) or click "Load Last Level.
I’m using the default character controller and I have tried using my own and even those found on unify and no solution has shown itself…
If I save the game and quit entirely, then reload the game and click “load last level” it works fine so part of me is thinking that I cannot go back in the build order, if that is the case, what is the solution?
Here is my player.js
//Shane O'Brien, 2012
var mainmenuSkin : GUISkin; //Skin
var areaWidth : float;
var areaHeigth : float;
var currentGUIMethod : Function;
var levelLoad : String;
function Start (){
//Start with the main menu GUI
this.currentGUIMethod = MainMenu;
}
function MainMenu(){
var ScreenX = ((Screen.width * 0.5)-(areaWidth * 0.5));
var ScreenY = ((Screen.height * 0.98)-(areaHeigth * 0.5));
GUI.skin = mainmenuSkin;
GUILayout.BeginArea(Rect(ScreenX, ScreenY, areaWidth, areaHeigth));
if(GUILayout.Button ("Play")){
Application.LoadLevel("1modern");
}
if(GUILayout.Button ("Load Last Level")){
levelLoad = PlayerPrefs.GetString("lastLevel");
Application.LoadLevel(levelLoad);
}
if(GUILayout.Button ("Options")){
//Options button clicked, swith to new menu
this.currentGUIMethod = OptionsMenu;
}
if(GUILayout.Button ("Quit Game")){
Application.Quit();
}
GUILayout.EndArea();
}
function OptionsMenu(){
GUI.skin = mainmenuSkin;
var ScreenX = ((Screen.width * 0.5)-(areaWidth * 0.5));
var ScreenY = ((Screen.height * 0.9)-(areaHeigth * 0.5));
GUILayout.BeginArea(Rect(ScreenX, ScreenY, areaWidth, areaHeigth));
if(GUILayout.Button ("Toggle Fullscreen")){
Screen.fullScreen=!Screen.fullScreen;
Screen.SetResolution (Screen.currentResolution.width, Screen.currentResolution.height, true);
}
if(GUILayout.Button ("Back to Main Menu")){
this.currentGUIMethod = MainMenu;
}
GUILayout.EndArea();
}
function None(){
}
function OnGUI(){
this.currentGUIMethod();
}
And here is my mainMenu.js
#pragma strict
//Shane O'Brien, 2012
var mainmenuSkin : GUISkin; //Skin
var areaWidth : float;
var areaHeigth : float;
var currentGUIMethod : Function;
var levelLoad : String;
function Start (){
//Start with the main menu GUI
this.currentGUIMethod = MainMenu;
}
function MainMenu(){
var ScreenX = ((Screen.width * 0.5)-(areaWidth * 0.5));
var ScreenY = ((Screen.height * 0.98)-(areaHeigth * 0.5));
GUI.skin = mainmenuSkin;
GUILayout.BeginArea(Rect(ScreenX, ScreenY, areaWidth, areaHeigth));
if(GUILayout.Button ("Play")){
Application.LoadLevel("1modern");
}
if(GUILayout.Button ("Load Last Level")){
levelLoad = PlayerPrefs.GetString("lastLevel");
Application.LoadLevel(levelLoad);
}
if(GUILayout.Button ("Options")){
//Options button clicked, swith to new menu
this.currentGUIMethod = OptionsMenu;
}
if(GUILayout.Button ("Quit Game")){
Application.Quit();
}
GUILayout.EndArea();
}
function OptionsMenu(){
GUI.skin = mainmenuSkin;
var ScreenX = ((Screen.width * 0.5)-(areaWidth * 0.5));
var ScreenY = ((Screen.height * 0.9)-(areaHeigth * 0.5));
GUILayout.BeginArea(Rect(ScreenX, ScreenY, areaWidth, areaHeigth));
if(GUILayout.Button ("Toggle Fullscreen")){
Screen.fullScreen=!Screen.fullScreen;
Screen.SetResolution (Screen.currentResolution.width, Screen.currentResolution.height, true);
}
if(GUILayout.Button ("Back to Main Menu")){
this.currentGUIMethod = MainMenu;
}
GUILayout.EndArea();
}
function None(){
}
function OnGUI(){
this.currentGUIMethod();
}
When I load a level I already have a player prefab in the scene, is it better practice to instantiate it on the level load?
Only learned Javascript in the past two months so if you spot anything I shouldn’t be doing please let me know =D