- Home /
Coin collection script (not working)
I'm creating a 2D game and made these scripts for three collectible coin types, gold, silver and copper. Each coin gameobject has a 2D box collider that is set to trigger. Each coin type is also attached to a UI text , one for coining gold coins, one for counting silver, and one for counting copper. The three UI text elements are attached to the one canvas that displays on the screen when run.
Gold coin script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class goldcoin : MonoBehaviour {
public GameObject gcoin;
public Text GcountText;
private int Gcount;
// Use this for initialization
void Start () {
Gcount = 0;
SetCountText ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D()
{
Destroy(gcoin);
Gcount = Gcount + 1;
SetCountText ();
}
void SetCountText ()
{
GcountText.text = "" + Gcount.ToString ();
}
}
Silver coin script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class silvercoin : MonoBehaviour {
public GameObject scoin;
public Text ScountText;
private int Scount;
// Use this for initialization
void Start () {
Scount = 0;
SetCountText ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D()
{
Destroy(scoin);
Scount = Scount + 1;
SetCountText ();
}
void SetCountText ()
{
ScountText.text = "" + Scount.ToString ();
}
}
Copper coin script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class coppercoin : MonoBehaviour {
public GameObject ccoin;
public Text CcountText;
private int Ccount;
// Use this for initialization
void Start () {
Ccount = 0;
SetCountText ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D()
{
Destroy(ccoin);
Ccount = Ccount + 1;
SetCountText ();
}
void SetCountText ()
{
CcountText.text = "" + Ccount.ToString ();
}
}
When the character walks into a gold coin, the coin disappears and the ui text displays '1', which is great. But when a silver coin or copper coin is collected, a '2' is displayed, which I don't want to happen. Then, when another coin is collected, of any type, the number either doubles or does nothing at all!
Please help!
Update: Every time I run the game, the number added to the UI text chnges, which I don't understand, because the script is adding '1' to the value each time...
Answer by Destolos · Aug 19, 2017 at 06:54 AM
So every coin inherits from one of these scripts, or is the variable gameobject always filled with the next coin? If you want to have the first point: The first thing, that has come to my attention is, that the counter variables are local per class, which means verey coin has its own counter. If you want to have one counter for each type you should use the static
keyword.
Thanks, I changed the int to a static int, and the gold coin now works! For some reason though, the silver and copper coins are doubling the variables. For each coin I collect, two is added to the counter.
Your answer
Follow this Question
Related Questions
How to check if BoxCollider2D collided with another BoxCollider2D? 2 Answers
How do I destroy a game object my character hit ONLY if a condition is active? 2 Answers
Particles only colliding with one of the player's two colliders? 0 Answers
Unity 2D Combat Text Issue With Multiple Enemies 2 Answers
Moving a ScrollRect child via script to focus on some point. 0 Answers