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 /
avatar image
-1
Question by connorwforman · Sep 15, 2017 at 04:12 PM · scripting-underthehood

Am I doing this correctly?

So will this script;

Detect when the bullet enters the enemy's collider

Subtract the bullet damage from the total enemy health

Destroy the enemy when the enemy health is less than or equal to 0:

 using UnityEngine; 
 using System.Collections; 
 
 public class M9Damage : Monobehaviour {
     
     public GameObject M9_Round; 
     public float Enemy_Health = 100.0f; 
     public float M9_Damage = 24.0f; 
     
     
     void EnemyDeath () 
     {
         Destroy.gameObject; 
     }
     
     
     void OnCollisionEnter (Collision col) 
     {
         if (co.gameobject.name == "M9 Round")
         {
             Enemy_Health -= M9_Round; 
             
         }
     }
     
     void Update () 
     {
         if (Enemy_Health <= 0) {
             EnemyDeath (); 
         }
     }
 }

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 PersianKiller · Sep 15, 2017 at 04:22 PM 0
Share

change

 if (co.gameobject.name == "$$anonymous$$9 Round") 

to

 if (col.gameobject.name == "$$anonymous$$9 Round")

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by LiterallyJeff · Sep 15, 2017 at 04:31 PM

The main issue I see is that:

 Destroy.gameObject;

Is not valid syntax.

It should be:

 Destroy(gameObject);

Secondly, you don't need to check health every frame, because health is not changing every frame. You can call a function like this to change the health value:

 private void TakeDamage(float damage)
 {
     Enemy_Health -= damage;
     if(Enemy_Health <= 0)
     {
         Enemy_Health = 0;
         EnemyDeath();
     }
 }
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 unit_nick · Sep 15, 2017 at 04:39 PM

 using UnityEngine; 
 using System.Collections; 
 
 public class M9Damage : Monobehaviour {
     // not required
     // public GameObject M9_Round; 
     public float Enemy_Health = 100.0f; 
     public float M9_Damage = 24.0f; 
 
 
     void EnemyDeath () 
     {
         Destroy.gameObject; 
     }
 
 
     void OnCollisionEnter (Collision col) 
     {
         // typo
         // if (co.gameobject.name == "M9 Round")
         if (col.gameObject.name == "M9 Round")
         {
             // wrong item
             // Enemy_Health -= M9_Round;
             Enemy_Health -= M9_Damage;
         }
     }
 
     void Update () 
     {
         if (Enemy_Health <= 0) {
             EnemyDeath (); 
         }
     }
 }
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 kaplica · Sep 15, 2017 at 05:03 PM

 using UnityEngine; 
  using System.Collections; 
  
  public class M9Damage : Monobehaviour {
      
      public GameObject M9_Round; 
      public float Enemy_Health = 100.0f; 
      public float M9_Damage = 24.0f; 
      
      
      void EnemyDeath () 
      {
          Destroy(gameObject); 
      }
      
      
      void OnCollisionEnter (Collision col) 
      {
          if (col.gameObject.name == "M9 Round")
          {
              Enemy_Health -= M9_Damage; 
              
          }
      }
      
      void Update () 
      {
          if (Enemy_Health <= 0) {
              EnemyDeath (); 
          }
      }
  }

Very simple code, but the way you are doing this will simply cause a lot of trouble once the game gets bigger, You have a damage script for a round, what happens if you get different types of rounds, or even different types of objects ? You will create a script for each type of object? That's a no-no in development world. You need to learn about polymorphism. I would suggest you to learn at least basics in C# before you jump in into creating more advanced games like FPS.

https://www.tutorialspoint.com/csharp/csharp_polymorphism.htm https://en.wikipedia.org/wiki/Composition_over_inheritance

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

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

71 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

Related Questions

ScriptableObject Inheritance Serialization failure 0 Answers

Understanding compiled code 0 Answers

Will a component destroyed after the FixedUpdate loop or the Update loop if Destroy(component) is called in FixedUpdate 0 Answers

How does "build" work? 1 Answer

How to use a C# framework/library with Unity 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