- Home /
score counter is not accurate
hello, I saw other articles but I didn't get the answer and I need to learn what is actually wrong with my script so any help would be nice for me since I am not very experienced yet. I made a script called score and placed it on the text object, made another one called collectcoin and placed it on coins, problem is when I move fast through the coins instead of increasing the score by one it is increased by an unpredictable value like 2, 3 or 1(when i move fast mostly 2 when i move slow mostly 1). in the end if I had 4 coins score counter shows 7,8 6 ! a value that does not help at all !just as a reminder :scorevalue is a static int so I can change it from each coin !any advice about what is wrong with my code or how to code more optimized would help a lot, tnx. score.cs
public class score : MonoBehaviour {
[SerializeField] Text text;
public static int scorevalue = 0;
// Use this for initialization
void Start () {
scorevalue = 0;
}
// Update is called once per frame
void Update () {
text.text = (scorevalue) + ": Score";
Debug.Log(scorevalue);
}
void OnTriggerEnter2D(Collider2D col)
{
}
}
collectcoin.cs placed on coins(multiple objects near each other, each one has a collectcoin.cs attatched to it)
public class collectcoin : MonoBehaviour {
Vector3 distance;
[SerializeField] GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update() {
}
}
void OnTriggerEnter2D(Collider2D col)
{ if(col.gameObject.tag=="Player")
{ score.scorevalue+=1
Destroy(this.gameObject);}
}
}
This was really annoying so i changed the collectcoin script into this :
void Update() {
float dist = Vector2.Distance(player.transform.position,this.transform.position);
if (dist <=.5)
{ score.scorevalue++;
Debug.Log(dist);
Destroy(this.gameObject);
}
even though third script works I need to learn my mistakes so that I won't repeat them, and by the way, my Player character is using a Rigidbody2D with continuous collision detection.
,
in your first coincollect script you are missing a semi-colin when you increase the score. after your bottom code block you are missing the f after .5 in your if statement. in c# you need an f after a decimal to delcaire a float.
Answer by prof · Nov 17, 2019 at 06:59 PM
private bool isCollected = false;
private void OnTriggerEnter(Collider other){
if (isCollected) return;
if (!other.gameObject.CompareTag("Player")) return;
isCollected = true;
Destroy(other.gameObject);
}
I assume OnTriggerEnter is called several times before gameObject gets destroyed. From docs:
Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.
Your answer
![](https://koobas.hobune.stream/wayback/20220612214840im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I change variable value in other script, so my UI score will be 0 1 Answer
score display not showing 1 Answer