coins not counted properly,Coins not counted properly
I am new to unity and C# I've made a simple game where random platforms gets generated and the user needs to move the ball on click (Clone of zigzag game) There are also diamonds generated at some points when the user collects them the score gets incremented by 10 if the game gets over i have a panel where score, highscore and diamonds will be displayed no problem here after returning to the home screen, i have a panel where highscore and no.of available diamonds will be visible the problem is number of diamonds are displayed as a random number every time which is supposed to be all the collected diamonds till now
this is my scoremanager.cs
public static ScoreManager instance;
public int score;
public int diamond;
public int totalDiamonds;
public int highScore;
void Awake()
{
if(instance == null)
{
instance = this;
}
}
// Start is called before the first frame update
void Start()
{
score = 0;
totalDiamonds = 0;
PlayerPrefs.SetInt("score", score);
}
// Update is called once per frame
void Update()
{
}
void incrementScore()
{
score += 1;
}
public void addDiamondScore()
{
score += 10; //adds 10 points for each collected diamond
}
public void startScore()
{
InvokeRepeating("incrementScore", 0.1f, 0.4f);
}
public void startDiamond()
{
diamond += 1; //adds diamond to collected diamond
}
public void stopScore()
{
CancelInvoke("incrementScore");
PlayerPrefs.SetInt("score", score);
PlayerPrefs.SetInt("dia", diamond);
totalDiamonds += diamond; // this is where the problem is. im adding the total diamonds which is 0 to the collected diamonds, so that every time the collected diamond will be added to the total. But if i collect x number of diamonds at the end, the total diamonds is 90,878,23,123,etc (any random number)
PlayerPrefs.SetInt("tdia", totalDiamonds);
if(PlayerPrefs.HasKey("highScore"))
{
if(score > PlayerPrefs.GetInt("highScore"))
{
PlayerPrefs.SetInt("highScore", score);
}
}
else
{
PlayerPrefs.SetInt("highScore", score);
}
}
Im calling the startDiamond() and addDiamondScore() in the ball controller.cs script when the ball collides with a diamond
Ball controller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallController : MonoBehaviour
{
[SerializeField]
public GameObject zigzag;
public GameObject particle;
public GameObject tapText;
public float speed;
Rigidbody rb;
bool started;
bool gameOver;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Start is called before the first frame update
void Start()
{
started = false;
gameOver = false;
}
// Update is called once per frame
void Update()
{
if(!started)
{
tapText.SetActive(true);
if(Input.GetMouseButtonDown(0))
{
rb.velocity = new Vector3(speed,0,0);
started = true;
GameManager.instance.StartGame();
}
}
Debug.DrawRay(transform.position, Vector3.down, Color.white);
if(!Physics.Raycast(transform.position, Vector3.down, 1f))
{
gameOver = true;
rb.velocity = new Vector3(0, -35f, 0);
Camera.main.GetComponent<CameraFollow>().gameOver = true;
GameManager.instance.EndGame();
}
if(Input.GetMouseButtonDown(0) && !gameOver)
{
SwitchDirection();
}
}
void SwitchDirection()
{
if(rb.velocity.z > 0)
{
rb.velocity = new Vector3(speed,0,0);
}
else if(rb.velocity.x > 0)
{
rb.velocity = new Vector3(0,0,speed);
}
}
void OnTriggerEnter(Collider c)
{
if(c.gameObject.tag == "Diamond")
{
GameObject parti = Instantiate(particle, c.gameObject.transform.position, Quaternion.identity) as GameObject;
Destroy(c.gameObject);//effect
Destroy(parti, 1f);//effect
ScoreManager.instance.addDiamondScore();//fun
ScoreManager.instance.startDiamond();//fun
}
}
}
Please help me to solve this issue im looking since last 6 hours Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613041918im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with coin script and operands 1 Answer
Increase in coins with json 0 Answers
Coin Collection Counter 2 Answers
Scoring system does not work properly 0 Answers