- Home /
Question by
birdiesawyer · Jun 04, 2015 at 03:17 PM ·
function update
Function not executing
I am trying to get the increased health to happen at every 500 points and it will do it on the first one but nothing after that. Can someone look at my code and see where I am going wrong?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class IncreasedHealthManager : MonoBehaviour {
public PlayerHealth startingHealth;
public PlayerHealth currentHealth;
public int scoreCounter = 0; // Every time you gain points on your score, increase scoreCounter by the same amount
Animator anim;
void Awake()
{
anim = GetComponent<Animator>();
}
void Start (){
}
void Update()
{
scoreCounter = ScoreManager.score;
if(scoreCounter >= 500)
{
anim.SetTrigger("HealthIncrease");
// Decrease scoreCounter by 500 and heal
scoreCounter = scoreCounter % 500;
currentHealth = startingHealth;
}
}
}
The score resets like it should be at that 500 or more mark it doesnt do it.
Thanks
Comment
Answer by allenallenallen · Jun 04, 2015 at 03:37 PM
I believe the best way to code this is actually have two separate variables in ScoreManager that add up the points at the same time. Also, % will get you the remainder when you divide scoreCounter by 500. I'm pretty sure that's not what you wanted.
int score = 0;
int scoreCounter = 0;
int currentHealth; // Set it yourself
int startingHealth; // Set it yourself
Animator anim;
// A void that handles gaining points, unless you already have something similar. If so, juse copy what's in this void.
void AddPoints(int addPoints)
{
// Both get the same amount of points each time.
score += addPoints;
scoreCounter += addPoints;
if (scoreCounter >= 500)
{
anim.SetTrigger("HealtIncrease");
scoreCounter -= 500;
currentHealth = startingHealth;
}
}