Level reload..?

sorry guys… i did try all the examples around here i could find…
but i just cant seem to figure it out.
it also doesn’t help that i am a total new b. (of course)

so… i have a level that if you lose… or die… or whatever…

you go right back to the beginning after everything is reset.

i have tried the , Application.LoadLevel(“Level_2”);

but it doesnt work like I would hope…

the level is TINY…
so i have no problem with taking a second or two to reload the scene from scratch…
but i cant seem to figure that part out…

any help would be totally the best…
and if you live in San Francisco ill even take you out for a drink, or an ice cream or something…

thanks!

feel free to talk to me like i dont know what im talking about… cause i dont.

Application.LoadLevel (Application.loadedLevel);

Thanks for the reply!

I just tried that out…
but it works just like the the regular Application.Load i was using…

it works on some stuff… like the geometry, but doesnt reset the GUITextures… or timers that i got going on.

i think im hoping for something that will act like a restart of the whole app…

so that absolutely everything goes back to where it was at the start of the scene.

Yes, it totally resets the level. Can you post the scripts that aren’t reseting properly. Because some things don’t get reset by a level load, like Time.time for instance.

Application load level does not reset stuff that`s global. So you have to reset static vars by hand for example. They are global to the whole app, not just one frame. And i guess same counts for your GUI stuff.

yeah thats totally it i think…
i can get around the GUI stuff…
but i am using time.time like this…

}

timeLeft = timeTotal - Time.time;
timeGUI.text = “” + timeLeft;

if (timeLeft <= 0)
{
yield gameLost();
}

// Wait for next frame
yield;
}

and then on my gameLost function i am loading a button to reset the level…

so how would i reset the timer by hand in this example?

i took the timer from the match game… but im thinking after looking around that maybe its not the simplest way to go…

swap out Time.time with Time.timeSinceLevelLoad

holy crud!

You are the smartest ever!
Today is officially Daniel day in San Francisco!

im gonna go run around the block screaming for joy now!

but seriously, thanks so much… that was the last thing i needed to figure out…

i think…

Hello I am trying a similar technique, where when the player dies he goes to a game over scene then theres a reset button, however when I press that to reload the level it jumps back to the game over menu, is there a way of loading it from the start of the game from that menu? I am trying this-

function OnMouseUp() {
Application.LoadLevel(1);

}

It also does the same if I go back to the main menu and press play after the game over

Check your application build settings; make sure the scene labelled 1 is the one you want. I once rebuild my project, added the scenes in the wrong order, and nothing worked for a while…

(If that is indeed your problem, you might consider loading the levels by name rather than index.)

Hi

I have sorted the scenes and they are the right number, however it is loading the scene from where I die(I think) as when I click on it automatically loads the game over scene, so this is how it plays out:-

Main menu- press play- loads level- die- game over- two options main menu or restart- on restart it goes to the level for like 2 seconds and reloads game over scene- this also happens when I go back to main menu and press play from the game over scene.

Hope that makes more sense.

It sounds like the level is loading correctly, but then whatever told it to put the game-over message up is still true.

For example, if you have some kind of controller object marked DontDestroyOnLoad. It will stay around even when a new level is loaded, or the same level is reloaded. Such an object would need to be destroyed or reset. Or maybe you are using static globals and aren’t resetting their values?

You could try sprinkling Debug.Log calls here and there to see what is being called, and figure out why the game thinks it is over.

Hey sorry im still new to this, could you explain?

I dont have any DontDestroyOnLoad.And would I reset static values?

It’s difficult to explain because I’m just throwing out guesses.

on restart it goes to the level for like 2 seconds and reloads game over scene

Somewhere you have a line of code, or a trigger, or something, that decides when the game is over. Maybe it’s when the player runs out of lives or health? Maybe it’s when a certain amount of time has elapsed?

When you are reloading the level, it sounds like the right level loads, but then the game is immediately over. So whatever was true when the game first ended is still true. Something is not resetting properly.

If what I’ve said so far isn’t enough to help you get unstuck, maybe you could tell us what exactly ends the game? If it’s a bit of code, could you post it?

Hey yeah your right about the game not resetting properly.

Here is the code when the health reaches zero the lives go down then goes to the game over screen.

var life1: Texture2D; //one life left
var life2: Texture2D; //two life left
var life3: Texture2D; //full lifes

static var LIVES = 3;

function Update ()
{

switch(LIVES)
{
case 3:
guiTexture.texture = life3;
break;

case 2:
guiTexture.texture = life2;
break;

case 1:
guiTexture.texture = life1;
break;

case 0:
Application.LoadLevel(2);

And my health script that relates to this goes as follows

function Die () {

// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[ ] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms)
var p : MonoBehaviour = b as MonoBehaviour;
if (p)
p.enabled = false;
//subtract life here
PlayerLives.LIVES -=1;
//fades screen to red when dead
LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.red, 1.0);

//explodes characters
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}

Hope that makes sense
break;
}
}

Looks like you need to set LIVES back up to three when the level is reloaded.

thanks I did try that by doing this

case 0:
Application.LoadLevel(2);
break;

but it didnt work however I did try this

function ApplyDamage (damage : float) {
if (hitPoints < 0.0)
return;

// Apply damage
hitPoints -= damage;

// Are we dead?
if (hitPoints < 0.0)
Die();
Application.LoadLevel(2);

}

That kind of works, it does what I want it to do, however now it kills the player with one shot.

If you want I could post the full health script if it will help.

Unless you meant added a seperate code that would reset LIVES so (this is a guess)

function Update (){

if LIVES= 0 Application.LoadLevel(2);

}

Your code formatting isn’t very clear. I recommend you always enclose the body of an IF statement in braces { }. And, indent the body as well.

You have two lines after the IF and since they aren’t surrounded by braces, only the first line is conditional. The 2nd line always runs. I’m pretty sure you meant this:

function ApplyDamage (damage : float) { 
    // Apply damage 
    hitPoints -= damage; 

    // Are we dead? 
    if (hitPoints < 0.0) {
        Die(); 
        Application.LoadLevel(2); 
    }
}

Hey I tried that but it has gone back to the orginal problem.

Heres the full health code
[/code]var maximumHitPoints = 100.0;
var hitPoints = 100.0;
var healthGUI : GUITexture;
var explosion: Transform;

private var healthGUIWidth = 0.0;
private var gotHitTimer = -1.0;

function Awake () {
healthGUIWidth = healthGUI.pixelInset.width;
}

function ApplyDamage (damage : float) {
// Apply damage
hitPoints -= damage;

// Are we dead?
if (hitPoints < 0.0) {
Die();

}
}

function Die () {

// Disable all script behaviours (Essentially deactivating player control)
var coms : Component[ ] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms)
var p : MonoBehaviour = b as MonoBehaviour;
if (p)
p.enabled = false;
//subtract life here
PlayerLives.LIVES -=1;
//fades screen to red when dead

LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.red, 1.0);

//explodes characters
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);

}

function LateUpdate () {
// Update gui every frame
// We do this in late update to make sure machine guns etc. were already executed
UpdateGUI();
}

function UpdateGUI () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0…1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

}

and heres the lives code

var life1: Texture2D; //one life left
var life2: Texture2D; //two life left
var life3: Texture2D; //full lifes

static var LIVES = 3;



function Update ()
{

switch(LIVES)
{
case 3:
guiTexture.texture = life3;
break;

case 2:
guiTexture.texture = life2;
break;

case 1:
guiTexture.texture = life1;
break;

//case 0:
//Application.LoadLevel(2);
//break;
}
}

The only other thing I can think of that may help is sending the zip file, sorry for being such a newb, you have been really helpful, and I really dont wanna p*** you off

The problem seems to be that reloading the level doesn’t reset LIVES. I think that’s because it’s static.

Is that lives script attached to a single object, or more than one object? If it’s only one object, try making LIVES non-static.

If you must have it be static, then you will need a line of code somewhere to set it back to 3. This might work:

function Awake  () {
   LIVES=3;
}

If this is on a single game object somewhere (the player or game-controller) it should be called every time the level is reloaded.

1 Like