awarding gold in mp, IE knowing who killed the enemy

I’m using click-to-move. I have my auto attack loop working so that the player moves into range and attacks until the enemy dies. When the enemy dies my script is still trying to find the enemy’s gameComponent Health which very quickly crashes unity. I want the enemy script to contain a function like “public void uponDeath tell player to stop attacking and give them gold.” Player knows who it is attacking based on mouse click. How does the enemy know who killed it?

I think i know a good way to do it but don’t know the right syntax. When i call the public void takeDamage in my enemy script by clicking on the enemy, the player sends its damage as a variable. Can the player also send a variable saying “this is my gameobject” so that the enemy can store it as a variable and call it later when the enemy dies. I set public void takeDamage(requires damage and gameObject) but don’t know how to tell player to declare itself as a variable.

Took me the entire day to figure out something so simple, I sent 2 variables to my enemy’s takeDamage function. I sent the player’s damage and “gameObject” then used it in the enemy script to reference back to the player. For anyone else struggling it looks like this:

Player has these things:
hit.transform.GetComponent ().TakeDamage (damage, gameObject);
public void stopattacking() {
firstclick = false;
}

public void gaingold (int amountOfGold) {
currentGold = currentGold + amountOfGold;
}

Enemy has this:

public void TakeDamage (int amount, GameObject player) {
enemycurrentHP = enemycurrentHP - amount;
healthBar.fillAmount = enemycurrentHP / enemymaxHP;
if (enemycurrentHP <= 0) {
player.transform.GetComponent ().stopattacking ();
player.transform.GetComponent ().gaingold (bounty);
Destroy (gameObject);
}
}

Im guessing you was following the uNet tutorial I recently tried the same thing started a few weeks ago :slight_smile:

Heres what I got

    public void TakeDamage(int amount)    {
        if (!isServer) { return; }

           health -= amount;
        if (isLocalPlayer)
        {
            if(UIHealthText != null)
            UIHealthText.text = health.ToString();
        }


        // audioc.audioClip = takeDamageSounds[1];
        CmdPlayAudioClip(0);
        if (health <= 0)
        {
            health = 0;
            if (isEnemyNPC)
            {
                //GIVE PLAYER EXP
                //CmdGIVEEXPv2();

                giveExp();

                Debug.LogError("DEAD");
                if (destroyOnDeath)                {
                    Destroy(gameObject);
                }

            }            else            {   //DESTROY PLAYER ON DEATH //NO
                if (destroyOnDeath)
                { //  Destroy(gameObject);
                    WWW test = new WWW("http://*******/sendMessege.php?ID=" + WWW.EscapeURL(GetComponent<PlayerConfig>().playerID.ToString()) + "&MYUSERNAME=" + WWW.EscapeURL(GetComponent<playerID>().playerUniqueName) + "&MESSEGE=" + WWW.EscapeURL("TESTING"));
                    StartCoroutine(sendDeathMessage(test));
                    giveExp();
                    health = maxHealth;
                    Respawn();
                }
                else
                {
                    WWW test = new WWW("http://*******/sendMessege.php?ID=" + WWW.EscapeURL(GetComponent<PlayerConfig>().playerID.ToString()) + "&MYUSERNAME=" + WWW.EscapeURL(GetComponent<playerID>().uniqueName) + "&MESSEGE=" + WWW.EscapeURL(" <color=red>died.</color>"));
                    StartCoroutine(sendDeathMessage(test));
                    giveExp();
                    health = maxHealth;
                    Respawn();
                }
            }
        }

    }
    [ServerCallback]
    public void giveExp()    {
        if (PlayerLastObject != null && isServer)        {
            playerLastHit = PlayerLastObject.GetComponent<PlayerConfig>();

            if (playerLastHit != null)            {
                int amountToGive = Random.Range(minExp, maxExp);         
                playerLastHit.recieveExp(amountToGive);
            }

        }
    }

I then added this script to an empty game object attached to the players weapon

public class weapon : NetworkBehaviour{
    Combat comb;

    void Awake() {
        comb = transform.root.gameObject.GetComponent<Combat>();
    }
    IEnumerator waitFor(float sec)    {

        yield return sec;
    }
 
    void OnTriggerEnter(Collider enemy)    {
        if (!comb.attacking)        {
            comb.attacking = true;
            comb.EnemyHit(enemy.gameObject);
            waitFor(transform.root.gameObject.GetComponent<PlayerMove>().cooldown);
            comb.attacking = false;

        }
    }
}
    [ServerCallback]
   public void EnemyHit(GameObject playerHit)
    {
      //  Debug.LogError("PLAYER HIT WITH COLLIDER");
        if (playerHit != null)
        {
            Combat _combat = playerHit.GetComponent<Combat>();
            if (_combat != null)            {
                    CmdPlayAudioClip(1);
                _combat.PlayerLastObject = this.gameObject;
                _combat.TakeDamage(10);
            }
        }
    }