Hi, I am making a simple 2d fighting game and health will decrease on one character while not doing so on the other. While printing health (2 different tests) the broken player will post this:
and the working one will post this:
The only difference is the order in which they will show the health. My script is this:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
bool iscollided = false;
public TextMeshProUGUI player1;
public TextMeshProUGUI player2;
int player1health = 100;
int player2health = 100;
public int playerNumber;
void Update()
{
if (Input.GetButtonDown("FirePrimary"+playerNumber.ToString()) && iscollided)
{
AttackPrimary();
}
player1.text = player1health.ToString();
player2.text = player2health.ToString();
}
void AttackPrimary()
{
if(playerNumber == 1)
{
player2health -= 5;
}
if(playerNumber == 2)
{
player1health -= 5;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
iscollided = true;
}
private void OnCollisionExit2D(Collision2D collision)
{
iscollided = false;
}
}
All my code is the same for both players; no errors appear in the editor.