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 PSTX · Mar 20, 2018 at 05:57 PM · c#scripting problemcollisionorder-of-execution

Detecting a collision and doing something to the colliding object

Hi, I'm pretty new to Unity and I'm trying to write a script that detects a collision with an enemy and applies damage specified in other scripts. The problem is, I think that the script executes the "DealDamage" function before even detecting the collision, so the script ends up not working at all. Please, Im stuck on this for a couple of days already lol.

 public class DamageDealerScript : MonoBehaviour {
     public GameObject Unit, Target;
     // Use this for initialization
     void Start () {
     }
     // Update is called once per frame
     void Update () { 
     }
     private void FixedUpdate()
     {
         Target = Unit;
     }
     void OnCollisionStay(Collision collision)
     {
             Unit = collision.gameObject;
             Debug.Log("Collision detected with: " + collision.gameObject.name.ToString());    
     }
     public void DealDamage(int amount, string type)
     {
         
         Debug.Log("Received Damage info with these properties: " + amount.ToString() + " , " + type);
         if (type == "Meele")
         {
                 Debug.Log("Preparing to deal Damage to:" + Target.name);
                 Target.gameObject.GetComponent<Bot_Movement>().TakeDamage(amount);
                 Debug.Log("Damaging Unit: " + Target.name + " for " + amount.ToString() + " damage");
                 Destroy(gameObject);
         }
     }
 }
Comment
Add comment · Show 3
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 TreyH · Mar 20, 2018 at 06:26 PM 0
Share

Nitpicking, it might be simpler to just reference that Bot_$$anonymous$$ovement component ins$$anonymous$$d of referencing the GameObject it's sitting in.

Can you expand on I think that the script executes the "DealDamage" function before even detecting the collision? $$anonymous$$aybe link the other script that you think is calling this prematurely? Clearly it's not being called here, so we can't help with that yet.

avatar image PSTX TreyH · Mar 20, 2018 at 06:48 PM 0
Share

There's not much to show with the other script. It just instantiates the damage dealer at a location and immidiately sends info about the damage amount and type to the instantiated damage dealer.

 `{
                         Instantiate(DamageDealer, new Vector3(BotPosRot.position.x, BotPosRot.position.y, BotPosRot.position.z - 1), BotPosRot.rotation);
                     }
                 }
                 GameObject[] CreatedDamageDealers = GameObject.FindGameObjectsWithTag("Dam_deal");
                 foreach (GameObject dealers in CreatedDamageDealers)
                 {
                     dealers.GetComponent<DamageDealerScript>().DealDamage($$anonymous$$eleeDamage, "$$anonymous$$eele");
                     Debug.Log("Sending Damage info to: " + dealers.gameObject.ToString());
                 }`

avatar image PSTX TreyH · Mar 20, 2018 at 08:00 PM 0
Share

I just Changed the script a bit, and apparently it doesn't detect the collision at all. I have no idea what's wrong, the colliders are there, they are just not working

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

233 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

Related Questions

Collision Detection Not Working (Unity 5.2.3) 5 Answers

Preventing A Teleporting GameObject From Passing Through Walls 2 Answers

Collision script doesnt work,OnCollisonEnter2D does not work if it collides with the enemy 2 Answers

Cannot detect trigger 2 Answers

Detect overlapping objects 2D game 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