Я хочу получить 10 очков, когда игрок убивает врага, но я не понимаю. Я создал метод для добавления оценки на мой скрипт PuntosPistas
в Player и просто вызываю метод внутри CheckHealth
Врага, но он не суммирует 10 очков в счете. Кто-нибудь может мне помочь? Это мой код:Усиление очков, когда игрок убивает врага
В 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();
}
}
}
В проигрывателе:
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);
}
}
Извините! Это полный код, где я называю 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
И это ошибка в консоли:
NullReferenceException: Ссылка на объект не указывает на экземпляр объекта Invector.v_AIMotor.CheckHealth () (в Assets/Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs: 504) Invector.v_AIMotor.TakeDamage (.Damage damage) (в Assets/Invector-3rdPersonController/Scripts/CharacterAI/v_AIMotor.cs: 582) UnityEngine .Component: SendMessage (String, Object, SendMessageOptions) vMeleeManager: OnDamageHit (HitInfo) (в Assets/Invector-3rdPersonCon Троллер/Сценарии/MeleeCombat/vMeleeManager.cs: 295) UnityEngine.Component: SendMessageUpwards (String, Object, SendMessageOptions) Invector.vHitBox: CheckHitProperties (Коллайдер) (на активы/инвекторов-3rdPersonController/скрипты/MeleeCombat/vHitBox.cs: 68) Invector.vHitBox: OnTriggerEnter (коллайдер) (в Assets/инвекторов-3rdPersonController/Scripts/MeleeCombat/vHitBox.cs: 48)
Где вы вызываете метод 'CheckHealth()'? –
Правильно отформатируйте свой код –