Problem with lives counter

Hi all - in my game a moving sphere can hit static cubes on the plane - the right cubes turn green and win you 1 life, the wrong cubes turns red and cost you 1 life.

Cubes have “Cube” as tag.

to change the color i use:

using UnityEngine;
using System.Collections;

public class REDCHANGECOLOR : MonoBehaviour {
    
    // Use this for initialization
    void Start () {
    
    }

    // Update is called once per frame
    void Update () {
    }
    void OnCollisionEnter(Collision Col) {

if(tag == "Cube") renderer.material.color = Color.red;

    }
}

and to count lives I use:

using UnityEngine;
using System.Collections;

public class LivesCounter : MonoBehaviour {
    int lives = 3;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
  	}

    void OnCollisionEnter(Collision Col) {

if(tag == "Cube") lives--;

else lives++;
		
	    GUIText guiLives = GameObject.Find ("guiLives").GetComponent();
	     guiLives.text = "Lives: " + lives;

    }
}

Cubes change colot all-right but the game does not work properly - in particular:

a) the counter does not count negatives, it counts +1 with any cube it touches
b) unexpectedly - once hit, some cubes determine a count of -2 points...

I am quite stuck on this, all help and/or suggestions would be very welcome - especially as I am new at this.
Thank you!

Alma

Presumably then, OnCollisionEnter is called multiple times when it collides with a cube - either a few times in a row or (less likely) a few times in the same frame. Try finding out if that’s happening with Debug.Log. If its caused by the former (the objects collide for several frames in succession), you may need to disable that cube’s collider (or the sphere collider but the box collider is the safer thing to disable) and re-enable it after a few frames.

When the sphere moves into the cube, especially through the middle of it, it is likely that it does not move completely out of the cube that frame. I’m guessing that -2 happens when the sphere moves straight through it.

Thank you LittleSettler for your help, it turned out that :
a) I was not properly stating my variables, I am having success with “public static int” with both lives and score
b) I added the counter to the scripts for the color and this seemes to have done the trick, now counting plus and minus just fine - off with the scoreboard now…

I did like your comments the time frame - the problem though I think had more to do with using a boolean true/false to turn off the cubes that were hit once already.

Thank you and

All the best!

Alma