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 /
This question was closed Apr 28, 2018 at 07:18 AM by HamzaAlrawi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by HamzaAlrawi · Apr 28, 2018 at 10:06 PM · collisioninstantiateoncollisionenterinstantiating

Adding OnCollisionEnter to instantiated object not working properly!

Hello,

Basically I am instantiating an object in a 2D game and adding a On Collision Enter script to it. However it is not triggering for some reason. Both objects have a Rigidbody2D component and a box collider

Script 1:

.......

 GameObject firedArrow =(GameObject) Instantiate (Arrow, firePointPosition, Quaternion.FromToRotation(Vector3.right,firePointPosition));
         firedArrow.AddComponent<ArrowDamageScript> ();
         if (playermiddleposition.x - firePointPosition.x >= 0) {
             speed = -speed;
             firedArrow.GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, 0f);
             speed = -speed;
         } else {
             firedArrow.GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, 0f);
         }
 ........


Script 2:

 void Start(){
         Debug.Log ("Script added!");
     }
     void OnCollisionEnter(Collision col) {
         ContactPoint  contpoint = col.contacts[0];
         Debug.Log ("We hit " + col.collider.name + " and did " + Damage + " damage.");
         
         Destroy (col.gameObject);
     }




I do get the debug message that the script was added. However, nothing is happening upon collision. Any ideas?

Comment
Add comment · Show 2
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 · Apr 28, 2018 at 07:09 AM 0
Share

dude if it's a 2d game you should change

  void OnCollisionEnter(Collision col) {
      ContactPoint  contpoint = col.contacts[0];
      Debug.Log ("We hit " + col.collider.name + " and did " + Damage + " damage.");
      
      Destroy (col.gameObject);
  }

to

  void OnCollisionEnter2D(Collision2D col) {
      ContactPoint  contpoint = col.contacts[0];
      Debug.Log ("We hit " + col.collider.name + " and did " + Damage + " damage.");
      
      Destroy (col.gameObject);
  }

also you have to attach a boxCollider2D to your object ,not boxCollider

avatar image HamzaAlrawi · Apr 28, 2018 at 07:16 AM 1
Share

Oh I feel so stupid!

Thank you very much!

*Note: the ContactPoint should also be ContactPoint2D

I really appreciate your help!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

198 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

Related Questions

Destroying instantiated game objects from prefab when coliding with the ground 1 Answer

Instantiating a tile in place of another tile 0 Answers

instantiating into array 2 Answers

OnCollisionEnter called while box colliders not touching 1 Answer

Game frozen with multiple prefabs instatiated. 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