- Home /
Problem is Solved
my lose health script isn't working?
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?
can you add a log to the trigger and check if its being called twice?
Answer by Sibz9000 · Mar 12, 2019 at 10:33 PM
Perhaps, sometimes the re-spawn position may be in the collider so gets and additional LosePoint() call. Could be fixed by ensuring the collider isn't at the re-spawn location when re-spawning, or disabling the collider on the player for short amount of time - sort of offering temporary invulnerability after a hit.
Follow this Question
Related Questions
Online multiplayer with a lot of physics interactions? 1 Answer
Non slippery movement 1 Answer
How to run a script to only 1 player in a multiplayer fps 1 Answer
Networked Physics 0 Answers
Taking a hit 3 Answers