Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Freeze and restart my game

Discussion in 'Scripting' started by Joralin, Mar 13, 2015.

  1. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Hello,

    in my game i use a countdown. When this countdown has finished, i want to freeze my game and display a "restart" button.

    By "clicking" this button the game should reload and reset everything.

    Its my first game with unity and i only found solutions that works "half" i would say.

    First i try to freeze my game with:
    Code (JavaScript):
    1. Time.timeScale = 0
    .

    This freezes the game, but my "reset button" also dont work now. I also dont know if this function is the best solution for me?

    The restart function also doesnt work like i want it to be. I tried:

    Code (JavaScript):
    1. Application.LoadLevel(0)
    ( I dont use any levels in my code, but i think this should reset everything).

    This works so far but it seems that this doesnt reset everything. I have a function that constantly moving prefabs. The problem is now, that this function is called before the first prefab is created and this causing errors.

    In my code i work with timers to avoid these behaviour.

    Maybe
    Code (JavaScript):
    1. Application.LoadLevel(0)
    doesnt reset the game time?


    Sorry for the long text, but i hope somebody can help me with this.
     
  2. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    I think that you must declare your own variable, and check it's enabled or disabled value to allow anything from happening in your game. If you set it so everything is enabled by default, when you call the Application.LoadLevel(Application.loadedLevel) will reload the same scene you're in, and reset all variables to default.
     
  3. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Hmm, but shouldnt the "application.loadlevel(0)" reset all variables by default? Have you an example how i have to change my function?
     
  4. AgathoSAreS

    AgathoSAreS

    Joined:
    Mar 14, 2015
    Posts:
    10
    Didn't think this to the end, but that might help partly: use the Reset() function.
    I forgot though if this function is called from unity when loading a scene or not.
    http://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html
    There's also a live tutorial about it from official unity tutorials.

    Remember to do instead of:
    public int myvariable = 0;

    this:
    public int myvariable;

    void Reset() {
    myvariable = 0;
    }
     
  5. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Thanks for your help guys but im a beginner and i dont understand nothing at all :-(.

    Why would setting an variable making any difference? This wouldnt reset my game to any other status than "application.Load.Level" does..


    When i use "application.Load.Level", all things i created in the "hierarchy" appears with the tag: "deleted gameobject".

    My backgrounds are still there but my gameobjects are not returning.

    It seems like unity destroys also my "Chief" Gameobjects that are not created with code but in the editor.

    How can i get them back?
     
    Last edited: Mar 14, 2015
  6. AgathoSAreS

    AgathoSAreS

    Joined:
    Mar 14, 2015
    Posts:
    10
    I'm not shure about this, but:
    double click the scene that is your level 0.
    Add your "Chief" gameobjects. (That should be there on initialisation of the scene.) Click File->Save Scene
     
  7. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    Hmm no, this just make my game having 2 objects of this kind. It also semms, that the "remainingTime" Variable is not reset trough "application.Load.Level(0)".

    And this variable is not a static one.

    Normally i can move my "main character"with mouseklicks, but after i use "application.Load.Level(0)" he is frozen.

    Does someone can explain me the overall behaviour of "application.Load.Level" exactly?


    Im really helpless with this :-(
     
  8. Joralin

    Joralin

    Joined:
    Aug 27, 2014
    Posts:
    30
    I found the problem.
    "Application.Load.Level" worked fine, the timers in my script were the problems. They were counting from the beginning of the game.

    So i replaced all "Time.time" with "Time.timeSinceLevelLoad".

    Thank you guys for trying to help me anyway :)