I am making a two player game (sort of like smash bros or gun mayhem ) it has two scripts that when one object enters the trigger collider it either destroys it if its a not a player or respawns if its a player and the player loses 1 health (that’s what is supposed to happen but the player will sometimes lose 1 health and other times the other player will lose 2 health) here are the two scripts:
the health script:
int curHealth = 5;
public Text health;
public GameObject me;
public Text winText;
public GameObject Green;
public GameObject Red;
void Start ()
{
winText.text = ("");
}
void Update ()
{
health.text = ("Health: " + curHealth);
if (curHealth <= 0) {
Invoke ("Death", 1);
health.text = ("Dead");
if (me == Red) {
winText.text = ("Green Wins!!");
Invoke ("NewScene", 1);
}
if (me == Green) {
winText.text = ("Red Wins!!");
Invoke ("NewScene", 1);
}
}
}
public void LosePoint ()
{
me.transform.position = new Vector3 (0, 3, 0);
//gameObject.SetActive (true);
curHealth -= 1;
}
void Death()
{
Destroy (gameObject);
}
public void NewScene ()
{
SceneManager.LoadScene(0);
}
the trigger collider script:
public Rigidbody2D rb;
public HSpriteSystem hScript;
public GameObject player;
void Start()
{
}
private void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Untagged") {
Destroy (other.gameObject);
}
if (other.tag == "Player") {
hScript = other.GetComponent<HSpriteSystem> ();
hScript.LosePoint ();
}
}
(the health script is called HSpriteSystem)
What’s Going Wrong? How could I fix it?