Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Elprofeta · Jul 09, 2017 at 09:59 PM · scripting problemdamagestatic-functionhealth

An object reference is required to access non-static member

hello all, im trying to make a health system for my game, te players runs with this script:

 public int maxHealth;
 public int currentHealth;
 
     void Start ()
     {
         currentHealth = maxHealth;
     }
     public void TakeDamage (int amount)
     {
         currentHealth -= amount;
     }

and i have a lazer prefab, with a script that handles the colision with the players and calls for the "take damage function"

     public int baseDamage;
     GameObject player;
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }
     void OnTriggerEnter (Collider other)
     {
            if (other.gameObject == player) 
         {
             HealthSystem.TakeDamage (baseDamage);
         }
     }
 }


i tryied to make it static, but it didn't help.

thanks in advance

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UnityCoach · Jul 10, 2017 at 02:29 AM

From what I understand, you want to reference the player's HealthSystem component.

Try this :

  public int baseDamage;
  GameObject player;
  HealthSystem healthSystem;
 
  void Start()
  {
      player = GameObject.FindGameObjectWithTag("Player");
      healthSystem = player.GetComponent<HealthSystem>();
  }
  void OnTriggerEnter (Collider other)
  {
         if (other.gameObject == player) 
      {
          healthSystem.TakeDamage (baseDamage);
      }
  }

}

Comment
Add comment · 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
0

Answer by Elprofeta · Jul 10, 2017 at 07:09 PM

i tryied that and didnt work, anyway after some more changes i finally make it work:

     public int baseDamage;
     GameObject disparo;
 
     void OnTriggerEnter (Collider other)
     {
         if (other.tag == "Player") 
         {
             HealthSystem healthRef = other.GetComponent<HealthSystem> ();
             healthRef.TakeDamage (baseDamage);
             Destroy (gameObject);
         }
     }

thank you @UnityCoach for pointing me in the right direction, that was really helpful.

Comment
Add comment · 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 UnityCoach · Jul 10, 2017 at 07:14 PM 0
Share

It makes even more sense this way. If you remove the tag check, and simply check for healthRef != null ins$$anonymous$$d, you end up with something very generic that applies damage to anything that can take some.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

148 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 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 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 make health and damage script function correctly? 3 Answers

Health and Damage Script Not Working 2 Answers

How do I make the object apply damage on trigger? 0 Answers

How to make my gun do damge and destroy a object? 1 Answer

Huge Health and Damage Script Problem 0 Answers


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