Search Unity

Problem with Scene Safe

Discussion in 'Scripting' started by unity_xAnmDpLX9JUzqA, Jan 20, 2020.

  1. unity_xAnmDpLX9JUzqA

    unity_xAnmDpLX9JUzqA

    Joined:
    Dec 25, 2018
    Posts:
    4
    Hi there I have a problem. I don't want the levels to reset when I finish a scene. Does anyone have a solution? Here I also have a Youtube video if you don't understand what I mean. Thanks in advance


    my scenes:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class ProgressBar : MonoBehaviour
    8. {
    9.     [SerializeField]
    10.     private Text text_1;
    11.  
    12.     [SerializeField]
    13.     private Text text_2;
    14.  
    15.     [SerializeField]
    16.     private Image bar_fill;
    17.  
    18.     [SerializeField]
    19.     private Image bar_outline;
    20.  
    21.     [SerializeField]
    22.     private Image circle_1;
    23.  
    24.     [SerializeField]
    25.     private Image circle_2;
    26.  
    27.     [SerializeField]
    28.     private Color color;
    29.  
    30.     [SerializeField]
    31.     private Color background_color;
    32.  
    33.     private int level = 0;
    34.  
    35.     private float currentAmount = 0;
    36.  
    37.     private Coroutine routine;
    38.  
    39.     void OnEnable()
    40.     {
    41.         InitColor();
    42.         level = 0;
    43.         currentAmount = 0;
    44.         bar_fill.fillAmount = currentAmount;
    45.         UpdateLevel(0);
    46.     }
    47.  
    48.     void InitColor()
    49.     {
    50.         circle_1.color = color;
    51.         circle_2.color = color;
    52.  
    53.         bar_fill.color = color;
    54.         bar_outline.color = color;
    55.  
    56.         text_1.color = background_color;
    57.         text_2.color = color;
    58.     }
    59.  
    60.     public void UpdateProgress(float amount, float duration = 0.1f)
    61.     {
    62.         if (routine != null)
    63.             StopCoroutine(routine);
    64.         float target = currentAmount + amount;
    65.         routine = StartCoroutine(FillRoutine(target, duration));
    66.     }
    67.  
    68.     private IEnumerator FillRoutine(float target, float duration)
    69.     {
    70.         float time = 0;
    71.         float tempAmount = currentAmount;
    72.         float diff = target - tempAmount;
    73.         currentAmount = target;
    74.  
    75.         while (time < duration)
    76.         {
    77.             time += Time.deltaTime;
    78.             float percent = time / duration;
    79.             bar_fill.fillAmount = tempAmount + diff * percent;
    80.             yield return null;
    81.         }
    82.  
    83.         if (currentAmount >= 1)
    84.             LevelUp();
    85.     }
    86.  
    87.         private void LevelUp()
    88.         {
    89.             UpdateLevel(level + 1);
    90.             UpdateProgress(-1f, 0.2f);
    91.         }
    92.  
    93.         private void UpdateLevel(int level)
    94.         {
    95.             this.level = level;
    96.             text_1.text = this.level.ToString();
    97.             text_2.text = (this.level + 1).ToString();
    98.         }
    99.     }
    100.  
    101.  
    102.  
    103.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestButton : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     ProgressBar bar;
    9.  
    10.     public void TestClick()
    11.     {
    12.         bar.UpdateProgress(0.1f);
    13.     }
    14. }
    15.  
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
  3. alihaghshenas821

    alihaghshenas821

    Joined:
    Aug 24, 2019
    Posts:
    3
    you can use playerprefs
     
  4. unity_xAnmDpLX9JUzqA

    unity_xAnmDpLX9JUzqA

    Joined:
    Dec 25, 2018
    Posts:
    4
    Playerprefs don´t work i tested
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When you stop play mode and start play mode again it is the same as if you had a build, launched the build, exited the build, then launched it again. If you want to have data persist between these separate runs of the game you need to serialize that data and save it somewhere. Then when you launch again you need to read that data back and put the data where it needs to go.

    You could save it to disk in some kind of text based or binary file, you could save it to PlayerPrefs, you could save it to a database, you could save it to some cloud service, etc, etc, etc. Figure out how exactly you want to do it, then try to do it. If you run into a problem you can then ask a specific question about exactly how you're trying to do it.

    Just saying PlayerPrefs don't work is just silly. The feature certainly does work, but it isn't some silver bullet that just handles everything for you. You still have to design your scripts to properly write the data as needed, read it back when your game launches, and act on that read data to put everything in your scene back to the same state it was when you saved the data.