1
\$\begingroup\$

I have a pause screen in my game having two buttons : resume game and exit to main menu The main menu have only 1 button wish is play, when i click on exit to main menu the main menu level is loaded,if then i click Play My game level scene is not restarting but continuing everything,having all it's pre created prefabs and all its game object.

How should I fix this particular bug and restarting the game when exit to main menu is clicked

Exit to Menu Button :

void OnMouseDown() {
    PauseGame.isPause=false;
    Application.LoadLevel("MainMenu");
}

Play button :

void OnMouseDown() {
    spriteRenderer.sprite = s2;
    Application.LoadLevel("Game");
}

enter image description here

enter image description here

I have three Prefabs:

Turret :

using UnityEngine;
using System.Collections;
public class Turret : MonoBehaviour {
// Use this for initialization
double LastBallTime=0.0;
double LastTurretMovingTime=0.0;
public double spawnballTime=1.5;
public double StartTurretMovingTime=35.0;
public double BallCount=2;
public bool IsRed=false;
public GameObject BallPrefab;
public GameObject RedBallPrefab;
Vector2 v ;

void Start () {
}

// Update is called once per frame
void Update () {
    if (Time.time > LastBallTime + spawnballTime) {
        LastBallTime=Time.time;
        int rand= Random.Range(1,6);
        if(rand>=1 && rand<=4)
            Instantiate(BallPrefab, transform.position, transform.rotation) ;
        else{
            Instantiate(RedBallPrefab, transform.position, transform.rotation) ;

        }

    }
    if (Time.time > LastTurretMovingTime + StartTurretMovingTime) {
        LastTurretMovingTime=Time.time;
        int x = Random.Range (-2,3);
        int y = Random.Range (-3,3);
        v = new Vector3 (x, y,0);
        gameObject.transform.position=v;

    }
}
void OnBecameInvisible ()
 {
    Destroy(gameObject);
 }
}

BALL :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Ball : MonoBehaviour {
// Use this for initialization
public static double Killed=0;
public Text ScoreTextPrefab;
 GameObject ParentCanvas;

void Start () {
    ParentCanvas = GameObject.Find ("Canvas");
}
void OnMouseDown() {
    if (!PauseGame.isPause) {
        Text t = Instantiate (ScoreTextPrefab, transform.position, transform.rotation) as Text;
        t.transform.SetParent (ParentCanvas.transform, false);
        if (gameObject.name.Contains ("RedBall")) {
                t.text = "O o P s";
                RedKilled++;
        } else {
                Killed++;       
        }
        Object.Destroy (gameObject);
    }



}
// Update is called once per frame
void Update () {

}


void OnBecameInvisible ()
{
    Destroy(gameObject);
 }
}

TurretBall manager wish is assigned for the main camera of the game scene :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TurretBallManager : MonoBehaviour {

// Use this for initialization

public GameObject TurretPrefab;
public double turretSpawnTime=35;
public double LastTurretTime=0;
public double MaxTurret=10;
int currentTurretCount=1;
public Text Killedtxt;
Vector2 v;

void Start () {
    v = new Vector2(0,2);
    Instantiate (TurretPrefab, v, Quaternion.identity);

}
void CreateTurret()
{
    if (Time.time > LastTurretTime + turretSpawnTime) {
        LastTurretTime=Time.time;
        currentTurretCount++;
        if(currentTurretCount>1)
            v.x=TurretPrefab.transform.position.x-currentTurretCount/2;
        if(currentTurretCount<=MaxTurret){
            Instantiate(TurretPrefab,v,Quaternion.identity);
        }

    }
}
// Update is called once per frame
void Update () {
    Killedtxt.text = "Kill : " + Ball.Killed;
    CreateTurret ();
  }
}
\$\endgroup\$
  • \$\begingroup\$ Is the pause screen a new level? \$\endgroup\$ – Ahkam Nihardeen Jan 8 '15 at 10:58
  • \$\begingroup\$ no it's just a panel in the canvas \$\endgroup\$ – Sora Jan 8 '15 at 10:59
  • \$\begingroup\$ When you go to the menu, and load back the level Game, it doesn't reset? And you want it to reset? \$\endgroup\$ – Ahkam Nihardeen Jan 8 '15 at 11:05
  • \$\begingroup\$ yes exactly . i want it to reset as you enter it for the first time (like restarting the game when i go back ) \$\endgroup\$ – Sora Jan 8 '15 at 11:06
  • \$\begingroup\$ There is the answer, though it might be what you are looking for \$\endgroup\$ – Ahkam Nihardeen Jan 8 '15 at 11:21
2
\$\begingroup\$

Loading a level from Application.LoadLevel should rest everything unless:

  • You have set that script to DontDestroyOnLoad
  • You have set some variables to be static

If it is any of those too, I would recommend manually editing the code so both should not interfere when loading from another level. In the case of the static variable, this can be done easily by setting it in the Awake Function

However, if you would simply like it to work, without editing the script in the game, there is a way. It is not the best way, but it should work fine. This does NOT work if some Game Objects are set to not Destroy on Load. They will still stay persistent. Edit the Play button script:

OLD SCRIPT

using UnityEngine;
using System.Collections;

public class Play : MonoBehaviour
    {
    void OnMouseDown() 
        {
        spriteRenderer.sprite = s2;
        Application.LoadLevel("Game");
        }
    }

NEW SCRIPT

OLD SCRIPT

using UnityEngine;
using System.Collections;

public class Play : MonoBehaviour
    {
    void OnMouseDown() 
        {
        spriteRenderer.sprite = s2;
        DontDestroyOnLoad(transform.gameObject);
        Application.LoadLevel("Game");
        Application.LoadLevel(Application.loadedLevel)
        Destroy(transform.gameObject);
        }
    }
\$\endgroup\$
  • \$\begingroup\$ The issue here is that you need to set the play button to nod Destroy when loading the Game level, and then destroying it after it does that job. This is because you need it to reset the level WHEN you are accessing that level Application.LoadLevel(Application.loadedLevel) \$\endgroup\$ – Ahkam Nihardeen Jan 8 '15 at 11:18
  • \$\begingroup\$ but isn't you like loading the game level twice ? like hitting f5 two times in a browser to refresh some webpage( this is an example to explain ) \$\endgroup\$ – Sora Jan 8 '15 at 11:40
  • \$\begingroup\$ Exactly. That is the primary issue here. Apllication.LoadLevel SHOULD reset anything that is not a static variable or you have set to not destroy on load. Since the game does not reset when you use Apllication.LoadLevel, I am guessing is that you are using static variables. The best thing you should do is use them sparingly, so they don't have this effect. If this is not possible, this is the only way, but it is not recomended. \$\endgroup\$ – Ahkam Nihardeen Jan 8 '15 at 11:51
  • \$\begingroup\$ um actually i am using static variable for the pause screen and the audioListener but i don't know why the prefabs are still alive even after i go back and play again ,this is weird ..... \$\endgroup\$ – Sora Jan 8 '15 at 11:55
  • \$\begingroup\$ Is only the prefabs still alive? \$\endgroup\$ – Ahkam Nihardeen Jan 8 '15 at 12:00
0
\$\begingroup\$

if you do happen to have a static variable, just initialize it again in either Awake() or Start (); then whenever you need to restart your level just call Application.loadedLevel

I was able to get it to work like this:

public static int aNumber;

void Awake ()
{
  int aNumberInitial = 5;
  aNumber = aNumberInitial;
}

void restartLevel () // Call this method to restart
{
  Application.loadLevel(Application.loadedLevel);
}
\$\endgroup\$

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.