- Home /
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##
Posted! just the part that involve taking coins, when i take it some times i take 1 and it shows 2, not only shows 2 on GUI, but actually totalCoins become 2 and not 1 for sure.
BIG WARNING, $$anonymous$$Y PLAYER HAS 2 COLLIDERS, ONE BOX2D AND ONE CIRCLE2D I THIN$$anonymous$$ IT'S THIS
Problem solved?
So its a wall rather than a floor, right?
http://forum.unity3d.com/threads/182046-2D-game-movement-issues-Stuck-On-Walls
This is getting very confusing now. Are you still getting multiple triggerEnters or is your problem getting stuck in the floor ? $$anonymous$$aaybe this is worth another question ?
If i understood right, you are collecting coins with the onTriggerEnter2D ? Give the coin-class a bool variable that tells you whether it is already collected or not perhaps ? Then it can't be collected twice in case the colliders are bouncing against eachother or you have multiple colliders in the player object
Answer by Alk Studios · May 29, 2014 at 08:44 PM
Try adding the collision detection to the coin objects and then use a boolean flag before destroying the game object.
private bool _collected = false;
void OnTriggerEnter2D(info : Collider2D)
{
if (info.tag == "Player")
{
if (!_collected)
{
totalCoins += 1;
_collected = true;// Not needed if destroying the game object
Destroy(this.gameObject);// Only use if you intend to destroy the game object
}
}
}
as i expected, your code has a logic problem, when collected it will never turn false again, i think i need to deter$$anonymous$$e it by Time.deltaTime, i don't know
If the object is destroyed as described in my provided code snippet, there is no need to change the value of collected as the object no longer exists. If however you wish to keep the object, you could simply test if the player is still collided, if the player is not, then simply change the value back to false.
Just to be clear, from the context of your original post, I assumed your game involved the collection of coins, after which a collected coin is no longer available. $$anonymous$$y snippet should work for such a situation. If I am wrong let me know and I'll provide a more appropriate answer :)
The problem is that your code is needed for every coin in the scene to detect the player, so if i have 5000000 coins, i have 5000000 scripts attached, if you read the first code that is part of my player movement script you will see that is one script attached to player that find coins. I understand your code, but it works only if the scripts is attached to coin object and search for player, then when destroyed your logic work, but for $$anonymous$$e not x.x
Your answer
Follow this Question
Related Questions
Running android app on version 4.0.3 crash? 0 Answers
Scripts don't work when i try app 2 Answers
Camera problem 2 Answers
Scene rendering problem on iOS and Android. 0 Answers
Shadows problem with mobile. 1 Answer