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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Coin Counter in multiple-level game

Discussion in 'Scripting' started by PhilJensen, Aug 26, 2022.

  1. PhilJensen

    PhilJensen

    Joined:
    Aug 5, 2021
    Posts:
    2
    Ive made a coin counter in my game, but everytime i get to the next level (scene), it resets the coin counter from the previous coins collected. Id also like to be able to, if you die in lvl.2, to still keep the coins from lvl.1 but the coins collected from lvl.2 at that point resets until you are on to the next level.

    Any help would be awsome, Thank you!

    This is the code so far:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class ItemCollector : MonoBehaviour
    8. {
    9.     int coins = 0;
    10.  
    11.     [SerializeField] Text coinsText;
    12.     [SerializeField] AudioSource collectionSound;
    13.  
    14.     private void OnTriggerEnter(Collider other)
    15.     {
    16.         if (other.gameObject.CompareTag("Coin"))
    17.         {
    18.             Destroy(other.gameObject);
    19.             coins++;
    20.             coinsText.text = "Coins: " + coins;
    21.             collectionSound.Play();
    22.         }
    23.     }
    24. }
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Use Additive scene loading to keep a constant single instance of your ItemCollector class, and then PlayerPrefs to make it persistent.
     
    PhilJensen likes this.
  3. PhilJensen

    PhilJensen

    Joined:
    Aug 5, 2021
    Posts:
    2
    Okay I'll try it out, Thank you!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    You can use additive scenes as Zal points out above, or the more traditional way of just making a GameManager.

    ULTRA-simple static solution to a GameManager:

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    OR for a more-complex "lives as a MonoBehaviour" solution...

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

    If you decide to go the additive scene loading route that is an entirely different approach. Here are some notes:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It's a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

    Two similar examples of checking if everything is ready to go:

    https://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713

    https://forum.unity.com/threads/uni...on-before-other-scripts.1153739/#post-7401794
     
    TheDevloper likes this.
  5. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    use a game manager and a shared coincounting variable, i prefer to use singletons in this kind of issue