Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

What is wrong with my code?

$
0
0
I was following the survival shooter tutorial on unity's website for their health and damage scripts so I can learn how to do it. So I followed it I understood it until when I completed the scripts I got an error: (42,34) CS0120 An object reference is required to access non-static member `DamageHandler.currentHealth` (43,39) CS0120 An object reference is required to access non-static member `DamageHandler.Damaged(int)` The thing I can't figure out is why is it saying I have not referenced it I did that in void awake. Here is my code if you want to look: using UnityEngine; using UnityEngine.UI; using System.Collections; public class DamageHandler : MonoBehaviour { public int startingHealth = 100; public int currentHealth; public float flashSpeed = 5f; public Color flashColour = new Color(1f, 0f, 0f, 0.1f); public Slider healthSlider; public Image damageImage; bool isDamaged; bool isDead; void Awake(){ currentHealth = startingHealth; } void Update(){ if (isDamaged) { damageImage.color = flashColour; } else { damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime); } isDamaged = false; } public void Damaged (int amount) { isDamaged = true; currentHealth -= amount; healthSlider.value = currentHealth; if(currentHealth <= 0 && !isDead) { Death (); } } void Death() { isDead = true; Destroy (gameObject); } } using UnityEngine; using System.Collections; public class EnemyShooting : MonoBehaviour { public float attackDelay = 0.5f; public int attackDamage = 10; GameObject player; DamageHandler damageHandler; bool playerInRange; float timer; void Awake () { player = GameObject.FindGameObjectWithTag ("Player"); damageHandler = player.GetComponent (); } void OnTriggerEnter2D (Collider2D other) { if (other.gameObject == player) { playerInRange = true; } } void OnTriggerExit2D (Collider2D other) { if(other.gameObject == player) { playerInRange = false; } } void Update () { timer += Time.deltaTime; if (timer >= attackDelay && playerInRange) { Attack (); } } void Attack () { timer = 0f; if(DamageHandler.currentHealth > 0) { DamageHandler.Damaged (attackDamage); } } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>