How to get attackerobject weapon name from onobjectimpact

Reza

Member
I tried to make an event detect what weapon I used like the example below but this didn't work, how should?



Code:
private void OnObjectImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
{
            Debug.Log(" impacted by " + attacker + " | " + attackerObject + " on collider " + hitCollider + ".");
            if(oldhit != hitCollider.gameObject)
            {
                count = 0;
            }
            
            if (attackerObject == "Sword" && hitCollider.GetComponent<OpsiveBridge>().IsDeath == true && count <= maxhit)
            {
                if (!isRandom)
                {
                    AddPickupToCollection(inventory.MainItemCollection);
                }
                else
                {
                    AddPickupToCollection(inventory.MainItemCollection, m_MinAmount, m_MaxAmount);
                }
                count += 1;
            }
            oldhit = hitCollider.gameObject;
}
 
attackerObject does indeed contain the name of the colliding object (weapon). So there may be something else not being checked correctly here - you may need to try logging some of the other values like IsDeath, count, etc. (I'm not sure where these are coming from with just this code snippet!)
 
Also make sure that you are comparing the name of the object to the correct string (e.g. if the sword is a prefab clone it may be "Sword (Clone)" instead). It's generally a good idea to check for an object's tag/layer/components rather than name, since name can vary in some situations.
 
I really want to check the tag object but the attackerObject parameter cannot search for tags or anything else so I'm confused what to do, for the death and count variable there is no problem I have checked it and it is safe
 
attackerObject is not a GameObject, so it doesn't have a tag component, so how do I get tags from what weapons do damage
 
attacker gets passed in as a GameObject. To access its tag:

if (attacker.tag == "MyTag") { ... }

Edit: My apologies, I mixed up attacker with attackerObject. attacker should be what you are looking for!
 
Last edited:
Sorry for the confusion.

Unfortunately at the moment there's no way to directly access the GameObject of the attacking melee weapon.

An alternative for now could be to check the character's currently active item in slot 0, like so:

C#:
    private void OnImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        Inventory inventory = attacker.GetComponent<Inventory>();
        Item item = inventory.GetActiveItem(0);
    }

And you should probably check that attacker is definitely a character first.

But unfortunately you still can't access the actual in-scene GameObject via the Item, only the visible object (item.GetVisibleObject()) or the drop prefab (item.DropPrefab). You could check if the name of the visible object contains the string "Sword", for example.
 
In fact, it turns out you can just cast attackerObject to a MonoBehaviour and access its GameObject that way, like so:

C#:
    private void OnImpact(float amount, Vector3 position, Vector3 forceDirection, GameObject attacker, object attackerObject, Collider hitCollider)
    {
        var attackerObjectGameObject = (attackerObject as MonoBehaviour).gameObject;
        Debug.Log(attackerObjectGameObject.tag);
    }
 
Top