- Home /
Duplicate Question.
http://answers.unity3d.com/questions/907412/ive-got-a-problem-with-my-coin-system-it-always-sh.html
Where is the Mistake in my Coin System?
I´ve got a question to a Coin System I created. The shown value is 1 and keeps 1 although it should be 0 at the beginning and should get bigger on touching coins. My Scripts are:
Player:
 using UnityEngine;
 using System.Collections;
 
 public class FigurBewegeDich : MonoBehaviour {
     public float moveForce = 300f;
     public float maxSpeed = 5f;
     public static int Coins = 0;
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void FixedUpdate () {
                 float direction = Input.GetAxis ("Mouse X");
                 if (direction != 0f) {
                         rigidbody2D.AddForce (
                 Vector2.right * direction * moveForce);
                         rigidbody2D.velocity = Vector2.ClampMagnitude (
                 rigidbody2D.velocity, maxSpeed);
                 }
 
         float direction2 = Input.GetAxis ("Mouse Y");
         if (direction2 != 0f) {
             rigidbody2D.AddForce (
                 Vector2.up * direction2 * moveForce);
             rigidbody2D.velocity = Vector2.ClampMagnitude (
                 rigidbody2D.velocity, maxSpeed);
         }
     }
 }
GUIText:
 using UnityEngine;
 using System.Collections;
 
 public class ShowCoins : MonoBehaviour {
     public int Coins1 = 0;
     public string Coins2="0";
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         Coins1 = PlayerPrefs.GetInt ("Coins");
         Coins2 = Coins1.ToString();
         guiText.text = Coins2;
     }
 }
Coins:
 using UnityEngine;
 using System.Collections;
 
 public class CoinScript : MonoBehaviour {
     public int Coins = 0;
     public float PosY = 0;
     // Use this for initialization
     void Start () {
         PosY = transform.position.y;
         transform.position =
 new Vector3(Mathf.Round (Random.value * 18 - 9),PosY,0);
     }
     
     // Update is called once per frame
     void FixedUpdate () {
     
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.name == "Figur") {
             PlayerPrefs.SetInt("Coins",Coins+1);
             Destroy (gameObject);
         }
     }
 }
Answer by MichalOp · Feb 22, 2015 at 09:02 PM
You are setting number of coins in playerprefs to Coins + 1, but not increasing coins count! You should do  if (other.name == "Figur") { Coins++; PlayerPrefs.SetInt("Coins",Coins); Destroy (gameObject); } 
Follow this Question
Related Questions
Death Counter Resets When Reloading Scene. 3 Answers
I´ve got a problem with my Coin System. It always shows 1 Coin. 2 Answers
Help with comparing what scene is loaded 1 Answer
Static variable error 2 Answers
[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                