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. Dismiss Notice

Score added on collision with another player

Discussion in 'General Discussion' started by ostrich160, May 1, 2014.

  1. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Hi guys, so I've got this very simple game going, and I guess for me to be able to ask the question I gotta give a bit of context. The game works by having a player made up of two parts, a front and a back (pictured)

    $Collision Design.png

    And attached to that back square, is the following code, which means that if something collides with it, the player is destroyed

    Now, heres the issue, that I cant for the life of me come up with a solution for. The game is Multiplayer, and all the players have this. I want to make it so when the front square hits the back square of another player (destroying the other player), the player that hit the player gets a point. Even better if you can tell me how to display this.

    Thanks a lot guys, hopefully there will be an easy answer to this, but I just dont know what to do.
     
    Last edited: May 2, 2014
  2. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    OK in the OnTriggerEnter you get the collider from this you can get the gameObject that collided and the Unit/Player script that will gain the points.

    So in OnTriggerEnter you will have a line like this...

    Code (csharp):
    1.  
    2. other.gameObject.GetComponent<Player>().AddScore(100);
    3.  
    Note just off the top of my head so you should check the docs to ensure you use the correct properties and methods.

    Also none player game objects could trigger this so you should really do this

    Code (csharp):
    1.  
    2. Player player = other.gameObject.GetComponent<Player>();
    3.  
    4. if (player) player.AddScore(1000);
    5.  
     
    Last edited: May 1, 2014
  3. ostrich160

    ostrich160

    Joined:
    Feb 28, 2012
    Posts:
    679
    Thanks mate, but Im having a little trouble understanding what this means, and how to implement it into my script, and I guess what to do from there to make it show as a gui text
    Sorry I dont specialise in coding, but I try