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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

programmatically initialized PolygonCollider2D resulting NullReferenceException upon accessing

Discussion in 'Scripting' started by drihpee, Mar 19, 2018.

  1. drihpee

    drihpee

    Joined:
    Apr 15, 2015
    Posts:
    16
    First thing first, I don't know if this is a bug or not or Collider2Ds classes are not meant to be created programatically ( through initialization ).

    So, I have some simple snippet script written like this

    Code (CSharp):
    1. var col = new PolygonCollider2D ();
    2. col.CreatePrimitive (4, Vector2.one, Vector2.zero);
    3. Debug.Log("Overlap :: " + Physics2D.OverlapCollider (col , new ContactFilter2D () { layerMask = this.ObstacleMask } , null));
    and when I test the script, I got
    Code (CSharp):
    1. NullReferenceException
    error referring to
    Code (CSharp):
    1. col.CreatePrimitive (...);
    Is there any workaround or approach without need to add the component to the game object ?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You can't use "new" on anything that inherits from monobehaviour. You need to use AddComponent<PolygonCollider2D>() to create and add it to a GameObject. AddComponent also returns the component that you are adding, so you can still modify it easily from that return value.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You also cannot pass null to the portion expecting an array in the method call.
    I believe you always have to set useLayerMask to true for your filter, for it to take the layers into consideration.
     
  4. drihpee

    drihpee

    Joined:
    Apr 15, 2015
    Posts:
    16
    Ah, I see, I've too much doing some traditional way ( not Unity way ).
    Although I'm doing that because I just need that
    Collider
    in that only moment / logic, but I guess I just need disable the component.

    Yeah, I forgot about that
    useLayerMask
    , but is it really can't do
    null
    on Array result ? I just need the return value of the function, but if that's really unavoidable I understand.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, it can't be null. In fact it has to be a size big enough for your needs, because the return value (from the docs) states that it's how many were overlapped and that could fit inside the array.
    You can re-use the array, though.. so, it won't be costing you much memory. :)
     
  6. drihpee

    drihpee

    Joined:
    Apr 15, 2015
    Posts:
    16
    Thanks for it, but now I'm confused, I always got "0" result from Physics2D.OverlapCollider(...), I want to know what causing this result of the OverlapCollider ? What I mean is, is the function returns 0 if no overlap collider, and if there overlapping it returns the count of the collider that overlap with ?


    EDIT :
    Nevermind, I've got it working now, I don't know what causing it but now it's working properly
     
    Last edited: Mar 22, 2018
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hmm.. It should return the count of overlapped colliders .. but also depends on the size of the supplied array.

    If you pass in an array of size 0, you will always get zero.
    If you pass in a size of 4 and there are 2, you should get two.
    if you pass in a size of 2 and there are > 2, you will only get 2.

    That's what I gathered from the docs :)

    Is that what you did, and no matter what you always got zero?
     
  8. drihpee

    drihpee

    Joined:
    Apr 15, 2015
    Posts:
    16
    Yeah something todo with the array size, I forgot to edit the value so I always got 0.