- Home /
Using start function after loading level a second time
Hey guys! So I have just created a main menu screen and when you select the first option, it loads my game scene. Then in my game scene, if you press 'p', it brings up a pause menu from which you can choose to return to the main menu. The only problem being that when the main menu is loaded, it doesn't work as it should.
When on the main menu, the currently selected item should move forward and rotate a bit. This all works fine when you are on the main menu for the first time, but when you go back to the main menu from game play, it doesn't work.
I think the problem lies with the fact that instead of using the Update() function on my main menu, I am using an infinite loop within the Start() function. Therefore when the main menu is reloaded, the script doesn't see it as only starting for the first time. I tried changing to Update() with no infinite loop and I also tried an infinite loop within the Awake() function, none of which worked. Had I tried harder going back to using the Update() function, it may have worked but I thought I'd see if anybody on here had any simpler fixes for my problem?
function Start() { menuItems[currentMenuItem].OnSelected(true);
while(true)
{
if(Input.GetAxisRaw("Vertical") > 0.9)
{
menuItems[currentMenuItem].OnSelected(false);
currentMenuItem--;
if(currentMenuItem < 0) currentMenuItem = 0;
menuItems[currentMenuItem].OnSelected(true);
yield new WaitForSeconds(keyDelay);
}
else if(Input.GetAxisRaw("Vertical") < -0.9)
{
menuItems[currentMenuItem].OnSelected(false);
currentMenuItem++;
if(currentMenuItem >= menuItems.length) currentMenuItem = menuItems.length - 1;
menuItems[currentMenuItem].OnSelected(true);
yield new WaitForSeconds(keyDelay);
}
yield;
if(Input.GetButtonDown("Jump"))
{
if(currentMenuItem == 0)
{
Application.LoadLevel(1);
}
if(currentMenuItem == 1)
{
Application.LoadLevel(2);
}
}
}
}
Thanks,
Matt