Hello Guest

Author Topic: Multiple Colliders get added on sprite automatically  (Read 4951 times)

pako

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Multiple Colliders get added on sprite automatically
« on: November 13, 2015, 07:24:14 pm »
Hello,

I'm using Unity 4.6.9f1, and the latest version of 2DToolkit.

I have a sprite animation and another single sprite in the scene.  The animation and single sprite belong to different collections, and they both have PolygonCollider2D attached inside the collection.  The sprite animation has a PolygonCollider2D for each sprite in the collection shaped according to each sprite.

Whenever I try to create a sprite in the scene, I get an error saying that the PolygonCollider2D has been destroyed but I'm still trying to access it (Error 2 attached).  Also when I click on the Play button, two things happen: a. I typically get a Null reference exception (Error 1 attached), and b. a new PolygonCollider2D gets added to both the animated sprite and the single sprite in the scene.  So, if I start and stop the Unity player 5 times, 5 extra polygon colliders get added to each sprite.  In other words, although the extra colliders get added at runtime, they are not removed when I stop the player.  Although my original "normal" colliders are set as triggers, the extra ones are not triggers.

Also, through a script I try to disable and then enable the polygon collider on the animated sprite. If the extra polygon collider gets added (usually), enabling/disabling of the original collider works properly.  However, there are some instances that the extra colliders don't get added, and then enabling/disabling of the original collider doesn't work.

Please tell me what might be wrong and how to fix it.

Thanks!

UPDATE:
I tried to reproduce these issues.  The 2 errors (attached snapshots) show up consistently.  However, the addition of multiple colliders does not happen consistently.  If I remove the problematic sprite instances and then recreate them, e.g. by dragging and dropping a prefab, colliders keep getting added every time I click on play.  However, this happens with the 2 specific prefabs, and not with others.  With other prefabs, if I remove the polygon collider, another one doesn't get added at all.

Additionally, the adding of extra colliders can start occurring without first removing the initial one.  It happened once, just after I had opened a collection, and then clicked play.  At that point, Unity hang, I had to restart it, and then colliders started adding up on the specific 2 instances.  However, I couldn't reproduce this.  I could reproduce with great probability Unity hanging after opening some (not all) of my collections, but not colliders being added after doing so.  So, I opened a "problematic" collection, clicked on Settings, and then clicked on one of the sprites in the collection, then clicked play, and Unity hang 90% of the time.

I'm really at a loss on what could be going wrong here, especially with colliders not properly acting like colliders.  I mean, I have an OnTriggerEnter2D event, the collider's "IsTrigger" is unchecked (gets unchecked by itself), but OnTriggerEnter2D still fires...

Obviously there's something seriously wrong with the setup, and if I hadn't spent many days setting it up, I'd just start from scratch, but that would mean losing many days (about 1-2 weeks actually) of work.

UPDATE 2:
I just discovered yet another way that multiple colliders get added.

First of all, I had tested the scene, and it seemed to behave more or less in a "stable" manner. Scene contents:
a. Player (animated sprite) with attached Rigidbody2D and PolygonCollider2D with IsTrigger enabled. Also attached a "Player" monobehaviour with OnTriggerEnter2D.
b. Power-Up GameObjects (single sprites) with only a  PolygonCollider2D and IsTrigger enabled (no Rigidbody2D attached).
c. A few Enemy GameObjects (animated sprite) that did not interact at all with the player in the present test.

When I clicked Play, OnTriggerEnter2D on the player fired only when it touched some of the Power-Up GameObjects, but not all of them, even though they are all set up the same.  However, there were no extra PolygonCollider2D added on any GameObjects.  This is why I say that it behaved more or less in a "stable" manner.

Then I thought that since OnTriggerEnter2D doesn't behave properly, maybe OnCollisionEnter2D will.  So, I commented out the OnTriggerEnter2D method and added a OnCollisionEnter2D method in the Player's Monobehaviour. I also disabled the IsTrigger on the player's collider.

I clicked Play and a PolygonCollider2D was added on every single GameObject in the scene that already had a collider on it.  Every time I stopped and then clicked Play again, an extra PolygonCollider2D was added on the affected GameObjects.

Additionally, OnCollisionEnter2D did not fire at all, when the Player went over the PowerUp GameObjects. I verified this, by placing a break-point inside the OnCollisionEnter2D method, and the break-point never got hit.

Interestinly enough, when the Player's collider was set as a trigger, the added extra colliders were NOT set as triggers, and when the Player's collider was NOT set as a trigger, the added extra colliders were set as triggers.

Here's the relevant code on the Player:

Code: [Select]
   
    //public void OnTriggerEnter2D(Collider2D other)
    //{
    //    if (other.tag == "PowerUp")
    //    {
    //        PowerUp newPowerUp = other.GetComponent<PowerUp>();
    //        StartCoroutine(ProcessPowerUp(newPowerUp));

    //        other.gameObject.SetActive(false);
    //    }

    //}

    public void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "PowerUp")
        {
            PowerUp newPowerUp = col.gameObject.GetComponent<PowerUp>();
            StartCoroutine(ProcessPowerUp(newPowerUp));

            col.gameObject.SetActive(false);
        }

    }

I also attach snapshots of the Inspector of the Player, PowerUp and Enemy GameObjects.

Some feedback and help would be really appreciated.
« Last Edit: November 15, 2015, 10:35:33 am by pako »