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
0
Question by firebird127 · Nov 20, 2018 at 02:44 AM · 2dscripting problemontriggerenter2denemydamage

Problems With Damaging Enemies on Trigger

Hi all, I'm having a lot of trouble trying to understand why i cant get my value for damage to when switching projectiles. I currently have 4 scripts that are called on a collision with a player projectile. Firstly, Collision Handling checks to see if the collision is a player projectile and subsequently gets the damage value associated with it from the projectiles script. Then it subtracts the damage value from the value for health in the Enemy Handling script. This is then checked in the next script (in this case "Grunt") to see if the value of EnemyHandling.health is less than or equal to zero, at which case the object will be deleted. The problem is, even with all of my projectiles having differing damages, the enemies are instantly deleted upon the trigger. It should be noted that none of the projectiles have a damage equal to or above the value for health. Any and all help will be greatly appreciated. Associated code below:

public class CollisionHandleling : MonoBehaviour {

 public float damage;
 public GameObject fireBallExplosion;
 public EnemyHandling enemy;

 void OnTriggerEnter2D(Collider2D col)
 {
    if (col.gameObject.tag == "Player Projectile")
     {
         if (col.gameObject.name == "fireBall")
         {
             Instantiate(fireBallExplosion, col.transform.position, col.transform.rotation);

             // Getter
             damage = col.gameObject.GetComponent<fireBall>().damage;
         }

         else if (col.gameObject.name == "axeMagic")
         {

             // Getter
             damage = col.gameObject.GetComponent<axeMagic>().damage;
         }

         else if (col.gameObject.name == "iceMagic")
         {
             Instantiate(fireBallExplosion, col.transform.position, col.transform.rotation);

             // Getter
             damage = col.gameObject.GetComponent<iceMagic>().damage;



             //Slow
             enemy.speed /= 2;
         }

         // Destroy projectile
         Destroy(col.gameObject);


         // Setter
         enemy.health -= damage;
         
         
     }
     
 }

}

public class EnemyHandling : MonoBehaviour { public float health = 100f; public float speed = 1f;

}

public class Grunt : MonoBehaviour {

 private float speed;
 private Rigidbody2D rb;
 private Vector2 Matrix;
 public EnemyHandling enemy;

 // Use this for initialization
 void Start () {
     rb = GetComponent<Rigidbody2D>();
     enemy = GetComponent<EnemyHandling>();
 }
 
 // Update is called once per frame
 void Update () {

     speed = enemy.speed;
     Matrix =new Vector2(speed, 0);

     // killing enemy
     // get health
     if (enemy.health <= 0f)
     {
         Destroy(gameObject);
     }
 }

 void FixedUpdate()
 {
     rb.MovePosition(rb.position - Matrix * Time.deltaTime);
 }

}

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 mguidry · Nov 24, 2018 at 03:51 AM 0
Share

I didn't notice any glaring issued with the code. Is it possible that the CollisionHandling script is on the wrong game object and you are destroying the enemy ins$$anonymous$$d of the projectile?

0 Replies

· Add your reply
  • Sort: 

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

243 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Adding jump animation in script editor [2D Project] 0 Answers

Save score across scenes 1 Answer

Sprite Renderer Not Changing Sprite via Script 1 Answer

Moving parent from child' script doesn't work 0 Answers

Question on 2d planet gravity 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