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

Coin and Life counter

Discussion in '2D' started by Kinfinite, May 15, 2020.

  1. Kinfinite

    Kinfinite

    Joined:
    Dec 9, 2019
    Posts:
    5
    Hi people,
    I was wondering if anyone had any idea how to keep a canvas displaying a value like a coin counter keeping your coins between levels. Mine reverts to 0 when I switch levels and only corrects after picking up another coin. I'll put the script i'm using below.
    Coin Manager:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;

    public class CoinManager : MonoBehaviour
    {
    public static CoinManager instance;
    public TextMeshProUGUI text;
    static int coincount;
    // Start is called before the first frame update
    void Start()
    {
    if (instance == null)
    {
    instance = this;
    }
    }

    public void ChangeScore(int coinValue)
    {
    coincount += coinValue;
    text.text = "X" + coincount.ToString();
    }
    }
    Coin Item:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    public class Coin : MonoBehaviour
    {
    public int coinValue = 1;

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.CompareTag("Player"))
    {
    CoinManager.instance.ChangeScore(coinValue);
    Destroy(gameObject);
    }
    }

    It's essentially the same system I'm using for the players lives
     
  2. Kinfinite

    Kinfinite

    Joined:
    Dec 9, 2019
    Posts:
    5
    Welp this is awkward. literally just copied the line
    text.text = "X" + coincount.ToString();
    to void start as well as where it already was.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Please use codetags, seems like this forum has been lately polluted by post from people who don't read the rules how to post messages... is there still some problem or did you solve it?
     
  4. Kinfinite

    Kinfinite

    Joined:
    Dec 9, 2019
    Posts:
    5
    Solved it, and I couldn't or dont know how to use tags, it said I couldnt add new tags or something like that
     
  5. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    That not that type of tag. When you're editing a reply, up on the edit format part you'll see a bunch of things (like B for Bold, I for Italics, and so on), among these you'll see Code: followed by an icon. Press that icon, and it pops up an interface to add in code snippets like this:
    Code (CSharp):
    1. public void SetSelectedPart(BuildingPart part)
    2.     {
    3.         DetailText.text = part.Description;
    4.         DetailImage.sprite = part.Sprites[0];
    5.         DetailImage.color = Color.white;
    6.         if (selectedPartPlacmentGO == null)
    7.             selectedPartPlacmentGO = Instantiate(GameResources.Current.BuildingPartPlacementPrefab);
    8.         BuildingPartPlacementController bppc = selectedPartPlacmentGO.GetComponent<BuildingPartPlacementController>();
    9.         bppc.BuildingPanelController_var = this;
    10.         bppc.SetBuildingPart(part);
    11.     }
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  7. GrizzlyPunchGames

    GrizzlyPunchGames

    Joined:
    May 21, 2020
    Posts:
    11
    You have to refresh your coin count when it is created (when new scene is created)
    i think if you will put this in Start() method will help:
    Code (CSharp):
    1. Start()
    2. {
    3. text.text = coincount.ToString();
    4. }