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

OnCollisionEnter2D error

Discussion in 'Scripting' started by Daviiid, Jul 4, 2022.

  1. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    Hi, I'm getting an error:
    ArgumentOutOfRangeException: Index and length must refer to a location within the string.
    Parameter name: length
    System.String.Substring (System.Int32 startIndex, System.Int32 length) (at <890d6fe26e8c408ea64b353e791fafce>:0)
    PlayerController.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/PlayerController.cs:139)

    I have a GameObject named HouseCollider, which acts as a barrier. It has a rigidBody2D with static body type and a box collider 2D
    and when it collides with the player it throws out this error.
    The player has a polygon collider that detects collisions and a circle collider that acts as a trigger to pick up collectibles.

    Here is the Collision code of the player:
    Code (CSharp):
    1. //COLLISION
    2.     private void OnCollisionEnter2D(Collision2D collision)
    3.     {
    4.         if (collision.gameObject != null)
    5.         {
    6.             GameObject collisionGameObject = collision.gameObject;
    7.             if (collisionGameObject.name.Substring(0, 13) == "PickUpsHealth")
    8.             {
    9.                 healthBar.value += healAmount * GlobalVariables.globalHealAmount;
    10.                 Destroy(collisionGameObject);
    11.             }
    12.             else if (collisionGameObject.name.Substring(0, 14) == "PickUpsBullets")
    13.             {
    14.                 bulletCountBar.value += bulletAmount * GlobalVariables.globalAmmoAmount;
    15.                 Destroy(collisionGameObject);
    16.             }
    17.             else if (collisionGameObject.name.Substring(0, 13) == "PickUpsShells")
    18.             {
    19.                 shellCountBar.value += shellAmount * GlobalVariables.globalAmmoAmount;
    20.                 Destroy(collisionGameObject);
    21.             }
    22.         }
    23.     }
    Collectible pick ups work without an issue, it's just when it collides with the house that I get the error and I have no idea why.
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    You are querying the substring with a fixed length, e.g. 14. If the name of the object you collide with is shorter it will fail.
    Using Strings isn't best practice anyway. You should dig into the concept of Layers.
     
  3. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    Nevermind I fixed it.

    Added an exception for the HouseCollider in script and changed the order of collision checks based on gameObject.name length from shortest to longest. So that if I collide with something that has a shorter name it would check it first.
     
  4. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    Thanks for the reply. I do have collectibles on a separate layer for collision detection, but I do need to distinguish them so I used the name for that.

    I could make a separate layer for all of them?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    There's no need to check if Collision2D.gameObject is null or not. You cannot get a callback for something that doesn't exist.