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 upariver · Jun 21, 2016 at 11:39 AM · instantiatecollision detectioninstantiate prefab

Collision Detection not working

I have a cube being fired from mouse location and I want it to do something when it hits a wall. Simple, right? Well, I have been stuck on collision detection for days now. I tried adding a script to the spawned cube, adding a tag to the collision GameObject, and write this code :

 using UnityEngine;
 using System.Collections;
 
 public class CakeCollisions : MonoBehaviour {
 
     public GameObject collisionWall;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnCollisionEnter(Collision col)
     {
         switch (col.gameObject.tag)
         {
             case "CollisionTag":
                 Debug.Log("Collided");
                 break;
         }
     }
 }

Does not work. Tried the other way around (Script on collision which is placed on level from start of game, and tagging the cube instead. Also doesn't work. Have been watching tutorials, following things by line, what am I doing wrong?

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 SoloDeveloper · Jun 21, 2016 at 02:59 PM 0
Share

do you have rigid body on this gameobject?

avatar image upariver SoloDeveloper · Jun 21, 2016 at 03:18 PM 0
Share

Yes, its a 2D game, however the cube is 3D and firing in X axis. Is it a RigidBody or RigidBody2D?

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by seckincengiz · Jun 22, 2016 at 06:00 PM

I understand what you are trying to do but you can achieve the same result just using OnTriggerEnter method. I wrote a simple example below. If this is not the case (If you must use collisions), make sure that one of your object has rigidbody component. Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. More Info : https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Also try this instead of using switch-case:

 if(col.gameObject.tag == "CollisionTag")
  {
     Debug.Log("Collided")
  }

OnTriggerEnter Method

     //Attach this script to the projectile
     //Make sure your wall has a tag in your case "CollisionTag"
     //Make sure that the wall has collider and it's Is Triggered proporty is checked.
     //If you also want collision effect; add another collider to your wall. And this time
     //Is Triggered proporty Unchecked
     
     //If you instantiate the projectile you should apply this script to root object and apply.
     
     using UnityEngine;
     using System.Collections;
     
     public class ExampleClass : MonoBehaviour {
         void OnTriggerEnter(Collider other) {
         //Note: we use colliders here, not collisions
             if(other.gameObject.tag == "CollisionTag"){
             Debug.Log("Collided");
         }
     }


Comment
Add comment · 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
2

Answer by Graphics_Dev · Jun 21, 2016 at 07:38 PM

You probably need to enable "Is Trigger" on the collider component.

Comment
Add comment · 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
1

Answer by BenCrew · Jun 21, 2016 at 03:04 PM

How fast are the cubes going? They may be going to fast and passing through the wall.

Comment
Add comment · Show 4 · 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 upariver · Jun 21, 2016 at 03:17 PM 0
Share

rb.velocity = Camera.main.transform.forward * 40;

avatar image BenCrew upariver · Jun 21, 2016 at 03:19 PM 0
Share

have you tried to reduce the speed to see if that helps? also have you tried break pointing and stepping through the code?

avatar image upariver · Jun 21, 2016 at 03:22 PM 0
Share

Just reduced the speed to 20, still doesn't work :/

avatar image BenCrew upariver · Jun 21, 2016 at 03:24 PM 1
Share

that's probably not the problem then. have you tried doing a general collision with "other" just to check its not just yours collision object you have a problem with?

avatar image
0

Answer by unity_ngP8_Fl6_Peiuw · Nov 27, 2018 at 10:20 PM

Check the layer that the offending object is on. I had the same sort of 'it isn't colliding' problem and none of the discussion mentioned layers. The object I was having problems with was assigned to the 'default' layer and everything else was on 'environment' or 'terrain' or 'player' and it wasn't until I asked the question about what layers are used for that I found out that in the Edit->project->physics menu there is a table of layers and you can select those that do or do not collide with each other. See https://docs.unity3d.com/Manual/LayerBasedCollision.html

Comment
Add comment · 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

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

68 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

Related Questions

Get a Insantiate particle system follow a GameObject 1 Answer

How to Change the Variables of an Instantiated Object? 0 Answers

Check if a large number of gameobjects are colliding 1 Answer

How to use Animator.Rebind ??? 0 Answers

question about operating on an instantiated object 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