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 npoguca · Oct 31, 2015 at 10:44 PM · 2dcollisionrigidbody

Detecting collision of two prefabs through code c#

Maybe I'm just dumb but I just can't figure it out. Say I've got two prefabs I'm initiating through code.

 Object obj = Resources.Load("poke");
 GameObject go = Instantiate(obj, new Vector3(), Quaternion.identity) as GameObject;
 GameObject gos = Instantiate(obj, new Vector3(), Quaternion.identity) as GameObject;

How do I detect any collision? Do I have to add a RigidBody? Is there a way to create a Collider through code? I keep finding the OnCollisionEnter() or something but there's no info about binding that event to a gameobject. Thanks in advance guys.

Also, please don't redirect me to some other answer cos I think I've tried all of them and got nothing.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ElDo · Oct 31, 2015 at 10:57 PM

Collision detection happens Independent of your spawning method. But at least one of your Instantiated GameObjects must have an non-kinematic Rigidbody for the OnCollision Events to happen (see here) of course your GameObjects Need some Kind of collider. If you use MeshColliders you Need to know that 2 non convex MeshColliders also don't Trigger Collision Events afaik.

Comment
Add comment · Show 8 · 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 npoguca · Oct 31, 2015 at 11:01 PM 0
Share

okay but how exactly do I refer the OnCollision event to a gameObject?

avatar image ElDo npoguca · Oct 31, 2015 at 11:07 PM 0
Share

In your C# script you have something like:

 void OnCollisionEnter(Collision collision){
 }
 void OnCollisionStay(Collision collision){
 }
 void OnCollisionExit(Collision collision){
 }

These are called at the respective Events (just like Start() or Awake() or Update()). OnCollisionEnter as soon as something starts to collide, OnCollisionStay as Long as something collides and OnCollisionExit as something stops to collide.

avatar image npoguca ElDo · Oct 31, 2015 at 11:17 PM 0
Share

I see. It just doesn't work for some reason. Both items have RigidBody, they collide alright. The events dont fire off

 item.obj.gameObject.AddComponent<Rigidbody2D>();
             item.obj.gameObject.AddComponent<Collider2D>();


also, the console says Cannot add component of type 'Collider2D' because it is abstract. Add component of type that is derived from 'Collider2D' ins$$anonymous$$d. UnityEngine.GameObject:AddComponent()

avatar image ElDo npoguca · Oct 31, 2015 at 11:27 PM 0
Share

Is there a reason why you have to add them by code? If it can't add the Collider there can't happen collision Events so you Need to check why they don't get added. try to add one of the here mentioned BoxCollider2D, CircleCollider2D, PolygonCollider2D or EdgeCollider2D. Then it should work.

 item.obj.gameObject.AddComponent<BoxCollider2D>();


avatar image npoguca ElDo · Oct 31, 2015 at 11:42 PM 0
Share

Yeah, I tried that already. :( Thanks anyway

Show more comments
Show more comments

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

52 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

Related Questions

2D RigidBody slightly penetrates objects causing a bounce effect. 3 Answers

Rigidbody2D figits when pushed against an object. 0 Answers

Why Isn't My OnCollisionEnter2D Code Not Working? 1 Answer

How do I get objects to detect collisions? 2 Answers

Problem with collisions 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