- Home /
OnControllerColliderHit counting twice towards score
I'm having trouble creating my score, I have been able to make it show up and add to it. The problem is that sometimes(most of the time) the player will go through the object and it'll be counted twice so instead of adding 50 points it adds 100.
using UnityEngine; using System.Collections;
public class Score : MonoBehaviour { public int currentScore; public GUIStyle scoreFont;
void OnGUI()
{
GUI.Label(new Rect(Screen.width - 100,20,100,50), "" + currentScore, scoreFont);
}
public void AdjustCurrentScore(int adj)
{
currentScore += adj;
if (currentScore < 1)
currentScore = 0;
}
public void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "Hamburger")
{
Destroy(hit.gameObject);
AdjustCurrentScore(+50);
}
}
}
This happens to me, i usually wrap a boolean and allow scoring while true and flip false on score and after the reset set to true again.
if (hit.gameObject.tag == "Hamburger")
{
if(canScore)
{
canScore=false;
Destroy(hit.gameObject);
AdjustCurrentScore(+50);
yield;
canScore=true; // Or reset this is the adjScore function
}
}
you may even be able to do away with the bool and say
if(hit.gameObject!=null)
{
//Destroy and add point
}
not sure if thatll work, but worth a shot
Your answer
Follow this Question
Related Questions
Show score when enemy is destroyed C# 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Please Help Me 1 Answer
Point system, something wrong 1 Answer