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 /
This question was closed Feb 14, 2018 at 03:52 AM by poppysilvers1997 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by poppysilvers1997 · Feb 14, 2018 at 12:39 AM · bulletdamagepublic variablehealth

Bullet subtracting damage from enemy health

So like I've spent the last hour searching around for how to fix this, and dispite writing down some peoples codes word for word character for character I can not figure this out.

So basically I have a bullet script, that makes the object move forward based on the objects public float, and a damage int to be applied to enemies.

public class bullet : MonoBehaviour {

 public int damage;
 public float velocity;

 void Update () {
     // bullet movement
     transform.position += transform.right * Time.deltaTime * velocity;
 }
     // bullet dealing damage, then destroying itself
     void OnCollisionEnter2D(Collision2D coll) {
     if (coll.gameObject.tag == "Enemy")
         coll.gameObject.GetComponent<EnemyHealth>().Health = Health - damage;
     Destroy (gameObject);

 }

}

it then in theory applies this damage effect to my enemy script:

public class EnemyHealth : MonoBehaviour {

 public int Health;

 void Update () {
     if (Health => 0f)
     {
         Destroy (gameObject);
     }
 }
 }

The bullet moves correctly and disapears on hit, but when I have the code to adjust the enemies health I get the error: EnemyHealth.cs(10,7) error CS1660: Cannot convert 'lambda' to non-delegate type 'bool'

There isn't a single bool anywhere in this code so I have no idea what it's talking about.

I also get the error: bullet.cs(17,57): error CS0103: The name 'Health' does not exist in the current context

so yeah I'm at a loss, any ideas?

Edit: Side note, I fixed the error "EnemyHealth.cs(10,7) error CS1660: Cannot convert 'lambda' to non-delegate type 'bool'" simply by changing the "health <= 0" to health < 1" It basically works the same since it's an int not a float, so that problem is fixed at least.

Comment
Add comment · Show 1
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 PlayCreatively · Feb 14, 2018 at 12:52 AM 0
Share

This line is producing your error

  coll.gameObject.GetComponent<EnemyHealth>().Health = Health - damage;

That single Health doesn't exist.

change the line to this

 coll.gameObject.GetComponent<EnemyHealth>().Health -= damage;

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by LiamofElites · Feb 14, 2018 at 12:57 AM

This can be made very easy thanks to the API SendMessage. I created a game like this too, so this should do it


bullet.cs

  public int damage;
  public float velocity;
  void Update () {
      // bullet movement
      transform.position += transform.right * Time.deltaTime * velocity;
  }
      // bullet dealing damage, then destroying itself
      void OnCollisionEnter2D(Collision2D coll) {
      if (coll.gameObject.tag == "Enemy")
          coll.gameObject.SendMessage("hit", damage);
      Destroy (gameObject);
  }


EnemyHealth.cs

  public int Health;
  
     void hit(int damage){
         Health -= damage;
         Debug.Log("Hit");
     }


Comment
Add comment · Show 6 · 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 poppysilvers1997 · Feb 14, 2018 at 01:09 AM 0
Share

So I made the mentioned changes, and things are at least working better, it runs without giving me an error, but the bullet still isn't dealing damage at all, I have the enemies health set to 5 and bullet damage set to 2 and no changes :/

(also side not I realised i'm dumb because I made the enemy die when health was above 1, not lower, so I fixed that)

thanks for the assistance so far though! nice to make some progress after like an hour of nothing

avatar image LiamofElites poppysilvers1997 · Feb 14, 2018 at 01:15 AM 0
Share

No problem! and have you tried declaring the health and damage in the awake method? I think that might work but if not you should post that part of the code and I'll take a look at it.

avatar image poppysilvers1997 LiamofElites · Feb 14, 2018 at 01:24 AM 0
Share

Not quit sure what you mean there, I don't have an awake section of that code yet. would I put public int health and damage in the void Awake? Sorry I'm still learning.

Show more comments

Follow this Question

Answers Answers and Comments

77 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

Related Questions

Health and Damage [C#] 2 Answers

My bullet script doens't work. 1 Answer

bullet damage problem 1 Answer

Health Regeneration 2 Answers

Health bar losing health 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