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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

awarding gold in mp, IE knowing who killed the enemy

Discussion in 'Scripting' started by NatalieBaldwin, Feb 23, 2017.

  1. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    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?
     
  2. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    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.
     
  3. NatalieBaldwin

    NatalieBaldwin

    Joined:
    Feb 18, 2017
    Posts:
    62
    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<enemyhealth> ().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<playercontrols> ().stopattacking ();
    player.transform.GetComponent<playercontrols> ().gaingold (bounty);
    Destroy (gameObject);
    }
    }
     
  4. Durins-Bane

    Durins-Bane

    Joined:
    Sep 21, 2012
    Posts:
    175
    Im guessing you was following the uNet tutorial I recently tried the same thing started a few weeks ago :)

    Heres what I got


    Code (CSharp):
    1.     public void TakeDamage(int amount)    {
    2.         if (!isServer) { return; }
    3.  
    4.            health -= amount;
    5.         if (isLocalPlayer)
    6.         {
    7.             if(UIHealthText != null)
    8.             UIHealthText.text = health.ToString();
    9.         }
    10.  
    11.  
    12.         // audioc.audioClip = takeDamageSounds[1];
    13.         CmdPlayAudioClip(0);
    14.         if (health <= 0)
    15.         {
    16.             health = 0;
    17.             if (isEnemyNPC)
    18.             {
    19.                 //GIVE PLAYER EXP
    20.                 //CmdGIVEEXPv2();
    21.  
    22.                 giveExp();
    23.  
    24.                 Debug.LogError("DEAD");
    25.                 if (destroyOnDeath)                {
    26.                     Destroy(gameObject);
    27.                 }
    28.  
    29.             }            else            {   //DESTROY PLAYER ON DEATH //NO
    30.                 if (destroyOnDeath)
    31.                 { //  Destroy(gameObject);
    32.                     WWW test = new WWW("http://*******/sendMessege.php?ID=" + WWW.EscapeURL(GetComponent<PlayerConfig>().playerID.ToString()) + "&MYUSERNAME=" + WWW.EscapeURL(GetComponent<playerID>().playerUniqueName) + "&MESSEGE=" + WWW.EscapeURL("TESTING"));
    33.                     StartCoroutine(sendDeathMessage(test));
    34.                     giveExp();
    35.                     health = maxHealth;
    36.                     Respawn();
    37.                 }
    38.                 else
    39.                 {
    40.                     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>"));
    41.                     StartCoroutine(sendDeathMessage(test));
    42.                     giveExp();
    43.                     health = maxHealth;
    44.                     Respawn();
    45.                 }
    46.             }
    47.         }
    48.  
    49.     }
    50.  


    Code (CSharp):
    1.     [ServerCallback]
    2.     public void giveExp()    {
    3.         if (PlayerLastObject != null && isServer)        {
    4.             playerLastHit = PlayerLastObject.GetComponent<PlayerConfig>();
    5.  
    6.             if (playerLastHit != null)            {
    7.                 int amountToGive = Random.Range(minExp, maxExp);        
    8.                 playerLastHit.recieveExp(amountToGive);
    9.             }
    10.  
    11.         }
    12.     }

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

    Code (CSharp):
    1. public class weapon : NetworkBehaviour{
    2.     Combat comb;
    3.  
    4.     void Awake() {
    5.         comb = transform.root.gameObject.GetComponent<Combat>();
    6.     }
    7.     IEnumerator waitFor(float sec)    {
    8.  
    9.         yield return sec;
    10.     }
    11.  
    12.     void OnTriggerEnter(Collider enemy)    {
    13.         if (!comb.attacking)        {
    14.             comb.attacking = true;
    15.             comb.EnemyHit(enemy.gameObject);
    16.             waitFor(transform.root.gameObject.GetComponent<PlayerMove>().cooldown);
    17.             comb.attacking = false;
    18.  
    19.         }
    20.     }
    21. }

    Code (CSharp):
    1.     [ServerCallback]
    2.    public void EnemyHit(GameObject playerHit)
    3.     {
    4.       //  Debug.LogError("PLAYER HIT WITH COLLIDER");
    5.         if (playerHit != null)
    6.         {
    7.             Combat _combat = playerHit.GetComponent<Combat>();
    8.             if (_combat != null)            {
    9.                     CmdPlayAudioClip(1);
    10.                 _combat.PlayerLastObject = this.gameObject;
    11.                 _combat.TakeDamage(10);
    12.             }
    13.         }
    14.     }
     
    Last edited: Feb 24, 2017