0

So i have 2 different scenes. One is the MainScene with MainMenu Panel, LevelSelection Panel, Settings Panel and Shop Panel. Other is the Level1 Scene. So on the Level1 scene i have a progress bar and coin reward for each finished level. On MainScene, LevelSelection Panel there is also a progress bar for each level and a global coin counter. How can i connect progress bars and coin systems from two scenes, so that when Level1 is finished, 5 coins are added on the LevelSelection Panel and progress bar also "progresses" for 1? Here is my script for the progress bar, i haven't made the coin one yet, but i have an idea for that too:

PROGRESS BAR:


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

public class ProgressBar : MonoBehaviour
{
    [SerializeField]
    private Image stars;

    [SerializeField]
    private Image stars_full;

    
    private int level = 0;

    private float currentAmount = 0;

    private Coroutine routine;

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
    }
    public void OnEnable()
    {
        level = 0;
        currentAmount = 0;
        stars_full.fillAmount = currentAmount;
        UpdateLevel(level);
    }

    public void UpdateProgress(float amount, float duration = 0.1f)
    {
        if (routine != null)
            StopCoroutine(routine);

        float target = currentAmount + amount;
        routine = StartCoroutine(FillRoutine(target, duration));
    }

    public IEnumerator FillRoutine(float target, float duration)
    {
        float time = 0;
        float tempAmount = currentAmount;
        float diff = target - tempAmount;
        currentAmount = target;

        while (time < duration)
        {
            time += Time.deltaTime;
            float percent = time / duration;
            stars_full.fillAmount = tempAmount + diff * percent;
            yield return null;
        }

        if (currentAmount >= 1)
            LevelUp();

    }

    public void LevelUp()
    {
        UpdateLevel(level + 1);
        UpdateProgress(-1f, 0.2f);
    }

    public void UpdateLevel(int level)
    {
        this.level = level;    
    }
}

And in my Level1 Control script i just call the UpdateProgress() method and it works. Also can i somehow stop the progress bar reseting when Level2 is loaded with DontDestroyOnLoad()?

Jovke
  • 15
  • 5
  • You should create an Inventory as ScriptableObject for instance. It would contain the data and your progress bar scripts are simply displaying the Inventory values. Then you can also increase/decrease the inventory and it will persist over scenes. Finally, you'd send the value to a server or store in PlayerPrefs to have persistence over sessions. – fafase Feb 15 '21 at 15:12
  • Any tutorial on how i can do that? – Jovke Feb 15 '21 at 15:25
  • Checkout the duplicate link. There I gave an example setup for the ScriptableObject reference ;) as alternative you can use `static` as also described there or finally use some sort of persistent data management such as `PlayerPrefs`, or XML/JSON to `FileIO` .. also described there ;) – derHugo Feb 15 '21 at 16:45
  • Okay, thank you a lot :D – Jovke Feb 15 '21 at 22:58

0 Answers0