Function not running everytime
Hey,
Im still learning and i bumped on a problem.
I learn by creating a simple game, something like space invaders. And the problem that i have is sometimes a script is executed and sometimes its not. Let me show you the code:
First my player fires a gun, it instantiates a prefab of bullet.
Fire a gun:
void Update() { if (Input.GetMouseButtonDown(0)) { Instantiate(rocket, strzal.position, strzal.rotation); } }
Bullet script:
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.tag== "Przeszkoda"){ collision.GetComponent().TakeDamage(damage); Destroy(gameObject); } }
Then if the bullet hits a target it triggers a damage function:
public void TakeDamage(float amount) { health -= amount; if (health <= 0) { ABC.GetComponent().NowyPoziomTrudnosci(); gameObject.SetActive(false);
And so far everything is working just fine. But problem starts with NowyPoziomTrudnosci function. Sometimes its running then 3 enemies are killed and nothing happens and 2 next enemies run the code and again x enemies die and code is not executing.
public void NowyPoziomTrudnosci() { kills = kills + 1; Debug.Log("wykonane"+ kills); }
Any idea where i made a mistake? I would understand if NowyPoziomTrudnosci wouldnt run at all. But its running, just not everytime.