I want to gain 10 points when the player kill an enemy but I don't get it. I've created a method to add score on my PuntosPistas
script in the Player, and just call the method inside of the CheckHealth
of the Enemy but it don't sum 10 points in score. Can anybody help me? This is my code:
In the Enemy:
public class v_AIMotor : vCharacter
{
GameObject Player;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
public void CheckHealth()
{
if (currentHealth <= 0 && !isDead)
{
isDead = true;
Player.GetComponent<PuntosPistas>().KillEnemy ();
print("10 points");
DisableActions();
}
}
}
In the Player:
public class PuntosPistas : MonoBehaviour
{
public int Score;
public Text TextoContador;
void Start ()
{
Score = PlayerPrefs.GetInt("Score");
TextoContador.text = "" + Score;
}
public void KillEnemy()
{
Score = Score + 10;
TextoContador.text = "" + Score;
PlayerPrefs.SetInt("Score",Score);
}
}
Sorry! This is the full code where I call CheckHealth():
#region AI Health
GameObject Player;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
public void CheckHealth()
{
if (currentHealth <= 0 && !isDead)
{
isDead = true;
Player.GetComponent<PuntosPistas> ().KillEnemy ();
DisableActions();
}
}
public void HealthRecovery()
{
if (currentHealth <= 0) return;
if (currentHealthRecoveryDelay > 0)
{
currentHealthRecoveryDelay -= Time.deltaTime;
}
else
{
if (currentHealth > maxHealth)
currentHealth = maxHealth;
if (currentHealth < maxHealth)
currentHealth = Mathf.Lerp(currentHealth, maxHealth, healthRecovery * Time.deltaTime);
}
}
protected void RemoveComponents()
{
if (_capsuleCollider != null) Destroy(_capsuleCollider);
if (_rigidbody != null) Destroy(_rigidbody);
if (animator != null) Destroy(animator);
if (agent != null) Destroy(agent);
var comps = GetComponents<MonoBehaviour>();
foreach (Component comp in comps) Destroy(comp);
}
public override void TakeDamage(Damage damage)
{
if (rolling || currentHealth <= 0) return;
if (canSeeTarget() && !damage.ignoreDefense && !actions && CheckChanceToRoll()) return;
// change to block an attack
StartCoroutine(CheckChanceToBlock(chanceToBlockAttack, 0));
// defend attack behaviour
if (canSeeTarget() && BlockAttack(damage)) return;
// instantiate hit particle
var hitRotation = Quaternion.LookRotation(new Vector3(transform.position.x, damage.hitPosition.y, transform.position.z) - damage.hitPosition);
SendMessage("TriggerHitParticle", new HittEffectInfo(new Vector3(transform.position.x, damage.hitPosition.y, transform.position.z), hitRotation, damage.attackName), SendMessageOptions.DontRequireReceiver);
// apply damage to the health
currentHealth -= damage.value;
currentHealthRecoveryDelay = healthRecoveryDelay;
// apply tag from the character that hit you and start chase
if (!sphereSensor.passiveToDamage && damage.sender != null)
{
target = damage.sender;
currentState = AIStates.Chase;
sphereSensor.SetTagToDetect(damage.sender);
if (meleeManager != null)
meleeManager.SetTagToDetect(damage.sender);
}
// trigger hit sound
if (damage.sender != null)
damage.sender.SendMessage("PlayHitSound", SendMessageOptions.DontRequireReceiver);
// update the HUD display
if (healthSlider != null)
healthSlider.Damage(damage.value);
// trigger the HitReaction when the AI take the damage
var hitReactionConditions = stepUp || climbUp || jumpOver || quickTurn || rolling;
if (animator != null && animator.enabled && !damage.activeRagdoll && !hitReactionConditions)
{
animator.SetInteger("Recoil_ID", damage.recoil_id);
animator.SetTrigger("HitReaction");
}
// turn the ragdoll on if the weapon is checked with 'activeRagdoll'
if (damage.activeRagdoll)
transform.SendMessage("ActivateRagdoll", SendMessageOptions.DontRequireReceiver);
CheckHealth();
}
#endregion
And this is the error in console:
NullReferenceException: Object reference not set to an instance of an object
Invector.v_AIMotor.CheckHealth () (at Assets/Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs:504)
Invector.v_AIMotor.TakeDamage (.Damage damage) (at Assets/Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs:582)
UnityEngine.Component:SendMessage(String, Object, SendMessageOptions)
vMeleeManager:OnDamageHit(HitInfo) (at Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vMeleeManager.cs:295)
UnityEngine.Component:SendMessageUpwards(String, Object, SendMessageOptions)
Invector.vHitBox:CheckHitProperties(Collider) (at Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vHitBox.cs:68)
Invector.vHitBox:OnTriggerEnter(Collider) (at Assets/Invector-3rdPersonController/Scripts/MeleeCombat/vHitBox.cs:48)
Please investigate on the way you call CheckHealth()
, it's not clear from your code.
That code should be work if you call the method properly, try in this way:
public class v_AIMotor : vCharacter {
GameObject Player;
void Start ()
{
Player = GameObject.FindGameObjectWithTag("Player");
}
public void CheckHealth()
{
if (currentHealth <= 0 && !isDead)
{
isDead = true;
Player.GetComponent<PuntosPistas>().KillEnemy ();
print("10 points");
DisableActions();
}
}
void Update()
{
CheckHealth();
}
}
Using Update
is not the best way for sure, but should be do the job.