Hey guys,
I’m pretty new to Unity and I’m trying to create an Air Hockey game. If a player hits the puck twice before the other player does, they lose a point. This works for my player but not for the ai and I’m not sure why. I also tried to include a lower limit so the score never goes below 0, but that is not working either.
Here’s the code:
private void OnCollisionEnter2D(Collision2D collision)
{
audioManager.PlayPuckCollision();
if (collision.gameObject.CompareTag("Player"))
{
HandleCollision("Player");
}
else if (collision.gameObject.CompareTag("AI"))
{
HandleCollision("AI");
}
}
private void HandleCollision(string collidedWithTag)
{
if (LastHit != null && lastCollidedWithTag == collidedWithTag)
{
if (collidedWithTag == "Player" && Score.eScore.PlayerScore != 0)
ScoreMinus.Decrement(Score.eScore.PlayerScore);
else if (collidedWithTag == "AI" && Score.eScore.AIScore != 0)
{
Debug.Log("Decrease AI");
ScoreMinus.Decrement(Score.eScore.AIScore);
}
}
LastHit = gameObject;
lastCollidedWithTag = collidedWithTag;
}
And this is the function I’m calling from my Score Script:
public void Decrement(eScore MinusScore)
{
if (MinusScore == eScore.PlayerScore)
PlayerScoreText.text = (--PlayerScore).ToString();
else
AIScoreText.text = (--AIScore).ToString();
}
Any help would be appreciated!