- Home /
Variable decreases multiple times
Hello, everyone! I have two scripts in different gameobjects. First one (short version)
public class GriffController : MonoBehaviour {
public int lifeCount = 4;
void OnCollisionEnter2D(Collision2D target) {
if (target.gameObject.tag == "DownTarget") {
lifeCount = lifeCount - 1;
}
}
}
Second one (short version)
public class EggController : MonoBehaviour {
public GriffController gf;
float lastTime = 0;
public float speed = 0;
public void Update() {
if (Input.GetMouseButtonDown(0)) {
speed = Time.time - lastTime;
lastTime = Time.time;
}
if (Input.GetMouseButtonDown(0) && speed < 0.2f && speed != 0) {
gf.lifeCount = gf.lifeCount - 1;
}
}
}
In the GriffController script, I reduce the lifeCount var when I collide with an object every 3 seconds.
In the EggController script, I check how often the player clicks on the mouse. If he does this often, I want to reduce the variable lifeCount, which is instantiated in the GriffController script.
I need this variable to change in both cases, but now the behavior is: I click very quickly, but the variable does not decrease. As soon as the condition from the first script is fulfilled, the variable is reduced by the number of clicks that I managed to make in three seconds and additionally - 1 as in GriffController.
How to make the variable decrease in addition when the player clicks too fast?
I think I mostly understand your problem. I tried both your scripts in a fresh project and the EggController clicking worked and was able to edit the GriffControllers lifeCount value. But it seems like for you the value is only updated when the GriffController is hit in a collision? If you have only given us the short version of your scripts I am guessing the problem is elsewhere within those scripts.