Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Shakthib08 · Aug 24, 2021 at 09:39 AM · coins

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

i want to generate random coin at runtime 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges