• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by gunman435 · Apr 12, 2016 at 01:44 AM · errorerror message

What is wrong with my code?

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<DamageHandler> ();
     }
 
     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);
         }
     }
 }
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by jgodfrey · Apr 12, 2016 at 01:48 AM

The offending code is here:

  if(DamageHandler.currentHealth > 0) {
          DamageHandler.Damaged (attackDamage);
  }

Those DamageHandler references (with a capital "D") are referring to the DamageHandler class. You want to refer to the DamageHandler instance you cached in the Awake() method. That's "damageHandler", with a lower-case "d".

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gunman435 · Apr 12, 2016 at 01:54 AM 0
Share

Yes it works now, thank you so much!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

61 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Fix: "Problem Validating Your License" 0 Answers

You must have verified your email address to create an avatar record? 0 Answers

CommandInvokationFailure: Unable to convert classes into dex format. 1 Answer

HELPP, unity 2021 doesn't work for me 1 Answer

error CS1525: Unexpected symbol `float' 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges