Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Aldabern · Dec 15, 2020 at 10:08 PM · collider2dfunctions

Function doesn't always work during collision

Hello,

this is my code on a prefab that is being instantiated during an attack

   [SerializeField] private float damageToGive;
     public GameObject Player;
 
     private void Update()
     {
         damageToGive = Player.GetComponent<PlayerDamageManager>().playerDamage;
 
     }
 
         private void OnTriggerEnter2D(Collider2D other){
 
         if(other.gameObject.tag == "Enemy")
         {
             other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
             
             Debug.Log("It collides"); 
         }
     }

And this is the health manager script on the enemy

     public float enemyCurrentHealth;
     
     public float enemyMaxHealth;
 
     //Functions
 
     public void HurtEnemy(float damageToGive){
 
         enemyCurrentHealth -= damageToGive;
         Debug.Log("Damage Done");
 
     }
 
     private void LateUpdate()
     {
         if(enemyCurrentHealth == 0f){
             Destroy(gameObject);
         }
     }

Debug.Log shows me that each collision is registered and HurtEnemy function is ran on each collision but it doesn't always subtract health and I can't understand why. :/ Any help on the subject would be appreciated.

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
1

Answer by rhapen · Dec 15, 2020 at 11:08 PM

it look alright except one thing you really should not do this in the update

  private void Update()
  {
      damageToGive = Player.GetComponent<PlayerDamageManager>().playerDamage;
  }

put it in the Start or Awake

It might ,it might not be cause. collision is happening in physics fixedupdate so it is out of sync with update. plus getting component in every update is of course very expensive.

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 Aldabern · Dec 17, 2020 at 10:52 AM 0
Share

I've tried changing it to Start() and Awake instead of Update. Weirdly enough it didn't work at first but reloading Unity made it work like intended.

Thank you so much! It means a lot to get help like that as a hobby weekend developer for fun. :)

avatar image
1

Answer by tategarringer · Dec 15, 2020 at 11:30 PM

I imagine you've stumbled upon Unity's Order of Execution for Event Functions! Your problem likely lies right here:


alt text


As you can see, OnTrigger events are called well before Update, so if you're making contact as soon as the object is instantiated you'll be calling your OnTrigger event before you've had a chance to define a value for damageToGive. I would recommend moving that line to a Start function or making it into a property instead!


 public GameObject Player;
 [SerializeField] private float damageToGive;
 
 private float damageToGiveProperty { get { return Player.GetComponent<PlayerDamageManager>().playerDamage; } }
 
 //You can do it this way
 private void Start()
 {
     damageToGive = Player.GetComponent<PlayerDamageManager>().playerDamage;
 }
 
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Enemy")
     {
         other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
 
         //OR this way with a property
         other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGiveProperty);
 
         Debug.Log("It collides");
     }
 }


orderofexecution.png (24.5 kB)
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 Aldabern · Dec 17, 2020 at 10:53 AM 0
Share

Thank you! This explains my problem so well. Big thank you, I've wasted at least 3h tinkering before asking.

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

144 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

Related Questions

how can I add the delay between different function calls in C#.. 3 Answers

Mixamo Animation and Animation Editor 1 Answer

when is pysics.raycast used, if it is called in Update() 1 Answer

Verification Exception with Custom JS Function 0 Answers

How to randomize the correct choice 2 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