Quantcast
Viewing all articles
Browse latest Browse all 49

Problem with duplicated score!

I'm using a script to add score, the script is simple it uses a OnTriggerEnter2D to detect player collider enter and add += 1 to the score, but some times if a pass faster on 4 coins for example it ads 6, 5, 7, it duplicate, i don't know why! C# This is the player script, can't show all but this is the score part: void OnTriggerEnter2D ( Collider2D other ){ if(other.tag == "coin"){ //If the player touches a coin, it will find the object that manages the coin count and tells it that a coin was taken. GameObject score = GameObject.Find("Score"); score.BroadcastMessage("getCoin", SendMessageOptions.DontRequireReceiver); //after this, the object is destroyed so it can't add more points again. Destroy(other.gameObject); } } This is the Score script that display how much totalCoins i have on gui. using UnityEngine; using System.Collections; public class Score : MonoBehaviour { //This is the sound we want to play if a player gets a coin. public AudioClip coinSound; //this is a private variable that we'll set as a saved variable in Start(). [HideInInspector] public static int totalCoins; void Start (){ //This sets the private variable to the saved variable we create thats also in this script. It allows us to carry on information that the player has changed by playing. totalCoins = 0; //This sets the Text in the top right Corner to display how many coins we have depending on the variable totalCoins. Ex. COINS: 16 transform.guiText.text = "COINS: " + totalCoins.ToString(); } //This function is called when a coin sends us the message "getCoin" which is in the coin.cs script. public void getCoin (){ //once we receive the message from the coin, we play the sound we set when a coin is taken. audio.PlayOneShot(coinSound); //we add 1 to totalCoins totalCoins += 1; //this updates the text in the top left corner again, just like in method Start() transform.guiText.text = "COINS: " + totalCoins.ToString(); } } **##BIG WARNING, MY PLAYER HAS 2 COLLIDERS, ONE BOX2D AND ONE CIRCLE2D I THINK IT'S THIS, BUT HOW TO THE SCRIPT DETECT 1 COLLIDER, OR HOW TO MAKE A 2d CHARACTER WITH 1 COLLIDER Oo##**

Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>