Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Add Multiple Box Colliders not Working

Discussion in 'Scripting' started by bgmsd1, Jul 30, 2018.

  1. bgmsd1

    bgmsd1

    Joined:
    Feb 8, 2016
    Posts:
    3
    Hello all. I am going crazy trying to figure this out. I want to add 2 colliders to a game object in code so that I can reference them by name in code. I want to know which collider was hit.

    Here is my code.

    Code (CSharp):
    1. public bool m_HandleCollisions;
    2.  
    3.     private BoxCollider2D boxColliderRight;
    4.     private BoxCollider2D boxColliderLeft;  
    5.  
    6.     private void Start()
    7.     {
    8.        
    9.         m_HandleCollisions = true;
    10.         boxColliderRight = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
    11.         boxColliderRight.offset = new Vector2(2.4f, -.03f);
    12.         boxColliderRight.size = new Vector2(.36f, 3.28f);
    13.         boxColliderRight.name = "ColliderRight"; //This collider gets the name ColliderRight at first and then after I create ColliderLeft, the right collider has the name ColliderLeft.
    14.  
    15.         boxColliderLeft = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
    16.         boxColliderLeft.offset = new Vector2(-2.55f, -.09f);
    17.         boxColliderLeft.size = new Vector2(.39f, 3.28f);
    18.         boxColliderLeft.name = "ColliderLeft";
    19.     }
    20.  
    21.     private void OnCollisionEnter2D(Collision2D coll)
    22.     {
    23.         if (m_HandleCollisions)
    24.         {
    25.             if (coll.gameObject.name.Contains("Character4") || coll.gameObject.name.Contains("Character5") || coll.gameObject.name.Contains("Character6"))
    26.             {
    27.                 GameObject gameObject = coll.gameObject;
    28.                 EnemyController EnemyControllerScript = gameObject.GetComponent<EnemyController>();
    29.  
    30.                 if (coll.otherCollider.name == "ColliderRight")
    31.                 {
    32.                     boxColliderRight.enabled = false;
    33.                     boxColliderLeft.enabled = true;
    34.                 }
    35.  
    36.                 if (coll.otherCollider.name == "ColliderLeft")
    37.                 {
    38.                     boxColliderRight.enabled = true;
    39.                     boxColliderLeft.enabled = false;
    40.                 }
    41.  
    42.                 EnemyControllerScript.ReverseDiection();              
    43.             }
    44.         }
    45.     }
    So the problem is in the Start method. I first create and name boxColliderRight. When I assign the name ColliderRight, the name is correct.

    Then I step through the code and when I assign the name "ColliderLeft" to boxColliderLeft, boxColliderRight has the name "ColliderLeft". How can 2 objects have the same name.

    I first noticed this in my collision event. When my object would hit what should be ColliderRight, it was going into the If for ColliderLeft. What am I missing?
     
  2. bgmsd1

    bgmsd1

    Joined:
    Feb 8, 2016
    Posts:
    3
    I figured out how to handle this. I still don't understand why this is happening, but at least I have a work around for it. After I posted this, I tried assigning tags to the colliders and check to see if I hit colliders with tag x or y, but that wasn't working either. So the solution was to use GetInstanceID(). See below.
    Code (CSharp):
    1. private BoxCollider2D boxColliderRight;
    2.     private BoxCollider2D boxColliderLeft;
    3.  
    4.     private int LeftInstance;
    5.     private int RightInstance;
    6.  
    7.     private void Start()
    8.     {
    9.         m_HandleCollisions = true;
    10.         boxColliderRight = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
    11.         boxColliderRight.offset = new Vector2(2.4f, -.03f);
    12.         boxColliderRight.size = new Vector2(.36f, 3.28f);
    13.         RightInstance = boxColliderRight.GetInstanceID();
    14.  
    15.         boxColliderLeft = gameObject.AddComponent<BoxCollider2D>() as BoxCollider2D;
    16.         boxColliderLeft.offset = new Vector2(-2.55f, -.09f);
    17.         boxColliderLeft.size = new Vector2(.39f, 3.28f);
    18.         LeftInstance = boxColliderLeft.GetInstanceID();
    19.     }
    20.  
    21.     private void OnCollisionEnter2D(Collision2D coll)
    22.     {
    23.         if (m_HandleCollisions)
    24.         {
    25.             if (coll.gameObject.name.Contains("Character4") || coll.gameObject.name.Contains("Character5") || coll.gameObject.name.Contains("Character6"))
    26.             {
    27.                 GameObject MyGameObject = coll.gameObject;
    28.                 EnemyController EnemyControllerScript = MyGameObject.GetComponent<EnemyController>();
    29.  
    30.                 if (coll.otherCollider.GetInstanceID() == RightInstance)
    31.                 {
    32.                     m_HandleCollisions = false;
    33.                     boxColliderRight.enabled = false;
    34.                     boxColliderLeft.enabled = true;
    35.                     EnemyControllerScript.ReverseDiection();
    36.                 }
    37.  
    38.                 if (coll.otherCollider.GetInstanceID() == LeftInstance)
    39.                 {
    40.                     m_HandleCollisions = false;
    41.                     boxColliderRight.enabled = true;
    42.                     boxColliderLeft.enabled = false;
    43.                     EnemyControllerScript.ReverseDiection();
    44.                 }
    45.             }
    46.         }
    47.     }