Script 1:
bool isDead = playerScript.TakeDamage(theFinalSide);
Player Script:
public int maxHP = 10;
public int currentHP;
private void Start()
{
currentHP = maxHP;
}
public bool TakeDamage(int damage)
{
currentHP -= damage;
if (currentHP <= 0)
{
return true;
}else
{
return false;
}
}
I have a dice where whenever the dice is rolled, the number that the dice lands on (“finalSide”) does damage to the player.
Problem:
- the player currentHP is not setting itself to maxHp eventhough I put it in void Start(), I have to manually set it to the maxHP in the player prefab
- the instantiated player is not taking damage but the prefab of the player takes damage
- When playing again, the player prefab’s currentHP sets itself to the previous game currentHealth instead of setting it to maxHP, so i have set it manually again
Full Script of Script 1:
IEnumerator PlayerAttack()
{
int randomSide = 0;
int finalSide = 0;
for (int i = 0; i <= 20; i++)
{
randomSide = Random.Range(0, 6);
diceSpriteRenderer.sprite = diceSides[randomSide];
yield return new WaitForSeconds(0.05f);
}
finalSide = randomSide + 1;
theFinalSide = finalSide;
if (nextTurn == 1) //player turn
{
bool isDead = enemyScript.TakeDamage(theFinalSide);
if (isDead)
{
battleStatus = BattleState.Victory;
EndBattle();
}
else
{
//battleStatus = BattleState.EnemyTurn;
nextTurn *= -1;
}
}
Debug.Log("PlayerDice:" + " " + theFinalSide);
Debug.Log("playerHP:" + " " + playerScript.currentHP);
Debug.Log("enemyHP:" + " " + enemyScript.currentHP);
Debug.Log(nextTurn)