Aquilla
Aquilla

Reputation: 23

unity 3D collider

Screen From the Game

i am trying to develop a melee combat game i am using edge collider and below code i noticed that when i move or enable and disable the collider from the inspector everything goes but when i stand in my palace and use AttackCol.enabled = !AttackCol.enabled; i cannot hit and the trigger function do not called
the only difference i see is the collider color when i add it from the inspector or while i am moving its color is normal but when i enable and disable it by code its color goes pale and do not do anything

public virtual void OnTriggerEnter2D(Collider2D collision)
    {
        if (DamageSources.Contains( collision.tag ))
        {
            StartCoroutine(TakeDamage());
        }
    }

Upvotes: 1

Views: 106

Answers (3)

Aquilla
Aquilla

Reputation: 23

i added AttackCol.isTrigger = AttackCol.enabled; after AttackCol.enabled = !AttackCol.enabled; and everything geos ok right now but i think it is a work around but i need to know why the color goes pale ?

Upvotes: 0

matthias_code
matthias_code

Reputation: 1013

Stuff like AttackCol.enabled = !AttackCol.enabled; is clever, but it can go wrong when that one is called (accidentally) more than once. I suggest trying it out in the simplest form AttackCol.enabled = true; to make sure the error is not there. Later you can still make it more elegant again! :)

Upvotes: 1

MohammadReza Arashiyan
MohammadReza Arashiyan

Reputation: 343


  • Make sure that your Trigger Game Object is not marked as Static.
  • Remove virtual from function definition.
  • Create another Game Object for Trigger Component and try to change activation of whole the Game Object, not collider component

Upvotes: 2

Related Questions