Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resetting the level without reloading it.

Discussion in 'Scripting' started by DanteDetlev, Oct 18, 2012.

  1. DanteDetlev

    DanteDetlev

    Joined:
    Sep 20, 2012
    Posts:
    8
    In my game level, i have a few gameobjects that have some children on it.
    Once the player interacted with it some stuff happens, like light color changes and such.

    Now in the menu, i want to make a "restart" button. At first i tried making it just reboot the whole level
    Code (csharp):
    1.  
    2. Application.LoadLevel("00_Main");
    3.  
    but i want to cut down the loading time.

    its just a few gameobjects i want to reset with the scripts that are attached to it also resetted.

    is there some way to do this trought tag or something?
    reset everything with tag "ResetMe" in some way?

    thank in advance
     
  2. markpdolby

    markpdolby

    Joined:
    Oct 15, 2012
    Posts:
    24
    I would suggest creating a generic reset script something like
    Code (csharp):
    1.  
    2.  
    3. public class ResetObject : MonoBehaviour{
    4.  
    5. public abstract void Reset();
    6.  
    7. }
    8.  
    9.  
    then extend this per object for example:
    Code (csharp):
    1.  
    2. public class BoxResetObject : ResetObject{
    3.  
    4. public override void Reset(){
    5. //reset parameters here
    6. }
    7.  
    8. }
    9.  
    Then have a reset manager class that contains an array of all the reset scripts in the scene and a function that you can call when you want to reset the scene such as:
    Code (csharp):
    1.  
    2. public class ResetManager: MonoBehaviour{
    3. public Reset[] _ResetScripts
    4.  
    5. public static ResetScene(){
    6. foreach(Reset r in _ResetScripts){
    7. r.Reset();
    8. }
    9. }
    10.  
    11. }
    12.  
    13.  
     
  3. DanteDetlev

    DanteDetlev

    Joined:
    Sep 20, 2012
    Posts:
    8
    so if i understand correct, you put this script to each object i want to have reset, and once the "master button" is pressed, it finds al the scripts and resets them ?
     
  4. markpdolby

    markpdolby

    Joined:
    Oct 15, 2012
    Posts:
    24
    Yeah, so you have the generic reset script which you extend per object, so if for example you have a light you want to reset then you would create a new script called LightResetScript which would extend from the generic Reset script and you would attach this script to the light you want to reset and add it to the array on the manager script that is placed in the scene. Then when you want to reset the scene call ResetManager.ResetScene(), from where the condition to reset is met, and the light will be reset to how you want it. Sorry if I haven't explained it very well.
     
    prakyath_unity likes this.