- Home /
The current score variable not updating when an enemy is hit (Using OnTriggerEnter2D function),Counter for current score not working with OnTriggerEnter2D function.
I have made a script called "Ball", attached to a sphere prefab. In this script, i have added the following lines of code - private void OnTriggerEnter2D(Collider2D col) {
if (col.gameObject.tag == "Enemy")
{
curr = curr + 1;
Debug.Log(curr);
Destroy(col.gameObject);
Destroy(gameObject);
}
}
Now the thing is, the curr variable is not updating as should be. The last two destroy statements are working as usual. I have used Debug.Log to print the updated value of curr variable, but console displays something like this -
This screenshot was taken when the sphere hit the "Enemy" 4 times. Can anyone explain what is happening and how I can rectify this? I just want the console to show the current value of curr variable.
Hi FIrst of all, why are you using a sphere and not a circle if it is a 2d game? I didn't understand well the problem. Is your problem that when you hit the enemy it does not detect well the player or only some times? Another thing... why do you destroy your player after he touch an enemy? If it is destroyed the score counter will stop if it isn't a static variable (or is it?).
Answer by raybarrera · May 20, 2018 at 07:18 PM
A few things come to mind:
Disable "Collapse" in the console to make sure the collapsed logs all say "1"
Check that nothing else in your script is affecting the "curr" variable.
Post the entire script for more detailed help.
Your answer
Follow this Question
Related Questions
Pause Time.time? 1 Answer
Can I change the destination of selecting console entries 0 Answers
How to save score for survival time? 1 Answer
Simple score system breaking crates 1 Answer