Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2d collision

Discussion in '2D' started by Sean-Powell, Jan 14, 2015.

  1. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Spawn_collision : MonoBehaviour{
    6.     public void OnCollisionEnter2D( Collision2D col){
    7.         if (col.gameObject.name == "item1") {
    8.             Debug.Log ("starting collision");
    9.                         //add points for item1
    10.                 } else if (col.gameObject.name == "item2") {
    11.                         //add points for item2
    12.                 } else if (col.gameObject.name == "item3") {
    13.                         //add points for item3
    14.                 } else if (col.gameObject.name == "name4") {
    15.                         //add points for item4
    16.                 }
    17.     }
    18. }
    i am using the above code to try detect 3 different items colliding with another 2d sprite, they are all 2d sprites yet they do not seem to collide. I have tried calling the class from start and making a overflow method and it just ran the overflow method and not this one. I need some help on how to call this method from the start method or on a different way to do this.
    Also will, col.GameObject.name even work with a sprite
     
  2. BlaXun

    BlaXun

    Joined:
    Jan 8, 2015
    Posts:
    52
    So, the event doesnt "trigger"?
    They all got a BoxCollider or some other collider?
    Do they all have a rigidbody2D?
     
  3. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    yes they all have a boxcollider2d and rigidbody2d sorry forgot to mention that
     
  4. BlaXun

    BlaXun

    Joined:
    Jan 8, 2015
    Posts:
    52
    Can you maybe post a screenshot?
    Is the "OnCollisionEnter2D" even called?
    Try debugging to check please.
     
  5. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    what would you like a screen shot off and i have tried a debug.log in it and it never got printed but when i try call the method it goes on about an overflow method and when i create an overflow method it just runs that method
     
  6. BlaXun

    BlaXun

    Joined:
    Jan 8, 2015
    Posts:
    52
    I'd just like to see the scene and one of those instances focused in the inspector...they r all the same, right?
    So, the OnCollisionEnter2D is not triggered? ....something must be wrong with the gameObject setup then. Screenshot would help. The objects are not kinematic, are they?
     
  7. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
  8. BlaXun

    BlaXun

    Joined:
    Jan 8, 2015
    Posts:
    52
  9. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    ok, i tried that and it still does not work, i am going to try creating an overflow method then calling it from start again
     
  10. Sean-Powell

    Sean-Powell

    Joined:
    Dec 18, 2014
    Posts:
    87
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Spawn_collision : MonoBehaviour
    6. {
    7.     public void Start(){
    8.         OnTriggerEnter2D ();
    9.         }
    10.     public Object OnTriggerEnter2D (Collider2D col)
    11.     {
    12.             Debug.Log ("starting collision");
    13.                 if (col.gameObject.name == "item1") {
    14.             Debug.Log("col");
    15.                 } else if (col.gameObject.name == "item2") {
    16.             Debug.Log("col");
    17.                 } else if (col.gameObject.name == "item3") {
    18.             Debug.Log("col");
    19.                 } else if (col.gameObject.name == "name4") {
    20.             Debug.Log("col");
    21.                 }
    22.         return col;
    23.     }
    24.     public Object OnTriggerEnter2D (){
    25.         string a = "1";
    26.         return col;
    27.     }
    28. }
    i have tried this overflow method and it is just going to the overflow method and not going to the method.