So I am having issues with my line for attack enemyHealth.currentHealth and error pops up is An object reference is requred for the non-static field,method, or property ‘enemyHealth.currentHealth’ First set code below one with error on line 33
the code im using as refereal is form the Unity Tutorial 3d survival shooter and i have looked over this few times i am unsure why i keep running into error unless simple oversight.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Attack : MonoBehaviour {
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
Animator anim;
GameObject player;
PlayerHealthBar cyclopsHealth;
// EnemyHealth enemyHealth; where does this even come from?
bool playerInRange;
float timer;
// Use this for initialization
void Awake () {
player = GameObject.FindGameObjectWithTag("Player");
cyclopsHealth = player.GetComponent<PlayerHealthBar>();
// enemyHealth = Get.Component <PlayerHealth>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
private void OnTriggerEnter(Collider other)
{
if(other.gameObject == player)
{
playerInRange = true;
}
}
void Update () {
timer += Time.deltaTime;
if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
{
Attacks ();
}
if(cyclopsHealth.currentHealth <= 0)
{
anim.SetTrigger("Death");
}
}
void Attacks()
{
timer = 0f;
if(cyclopsHealth.currentHealth > 0)
{
cyclopsHealth.TakeDamage(attackDamage);
}
}
}
this is enemyHealth code that it should be calling it from
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class enemyHealth : MonoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public float sinkSpeed = 2.5f;
public int scoreValue = 10;
public AudioClip ScreamsShouts_monster_processed_051;
Animator anim;
AudioSource enemyAudio;
ParticleSystem hitParticles;
CapsuleCollider capsuleCollider;
bool isDead;
bool isSinking;
// Use this for initialization
void Awake()
{
anim = GetComponent<Animator>();
enemyAudio = GetComponent<AudioSource>();
hitParticles = GetComponentInChildren<ParticleSystem>();
capsuleCollider = GetComponent<CapsuleCollider>();
currentHealth = startingHealth;
}
// Update is called once per frame
void Update () {
if (isSinking)
{
transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime);
}
}
public void TakeDamage (int amount, Vector3 hitpoint)
{
if (isDead)
return;
enemyAudio.Play();
currentHealth -= amount;
hitParticles.transform.position = hitpoint;
hitParticles.Play();
if(currentHealth <= 0)
{
Death();
}
}
void Death()
{
isDead = true;
capsuleCollider.isTrigger = true;
anim.SetTrigger("Dead");
enemyAudio.Play();
}
public void StartSinking()
{
GetComponent<NavMeshAgent>().enabled = false;
GetComponent<Rigidbody>().isKinematic = true;
isSinking = true;
//not inplumented yet ScoreManager.score += scoreValue;
Destroy(gameObject, 2f);
}
}