Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Identifying which player activated the trigger, Photon

Discussion in 'Scripting' started by nijecogi, Jul 8, 2020.

  1. nijecogi

    nijecogi

    Joined:
    Sep 11, 2019
    Posts:
    3
    Hi guys,
    i'm pretty new to network programming but i'm not new to Unity.
    As of now i'm spawning a player prefab for each player (2 players) and I want them to be able to pick up points, which are spheres with isTrigger activated.
    How can I identify which of the two players in the server picked up the pointSphere and add the points accordingly ?
    I would also like ti display the points of both players on both of the screens with player names + point count.
    Any help is appreciated
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Just an idea...

    static class with some data.
    Code (CSharp):
    1. public static class GameManager
    2. {
    3.     public static List<PlayerData> playersData = new List<PlayerData>();
    4. }
    At PlayerData, attached to each player:
    Code (CSharp):
    1. public class PlayerData
    2. {
    3.     public string playerName;
    4.     public float/int score;
    5.     void Awake()
    6.     {
    7.         GameManager.playersData.Add(this);
    8.     }
    9. }
    At spheres:
    Code (CSharp):
    1. public class Sphere
    2. {
    3.     void OnTriggerEnter2D(Collider2D col)
    4.     {
    5.         if (col.transform.CompareTag("Player"))
    6.             col.gameObject.GetComponent<PlayerData>().score++;
    7.             Destroy(gameObject);
    8.     }
    9. }
    U can get all your player scores with some like:
    Code (CSharp):
    1. foreach(PlayerData playerData in GameManager.playersData)
    2. {
    3.     print(playerData.playerName + ": " + playerData.score.ToString());
    4. }
     
    Last edited: Jul 8, 2020
    roused2 and nijecogi like this.
  3. nijecogi

    nijecogi

    Joined:
    Sep 11, 2019
    Posts:
    3
    Thanks for the answer man, I tried to implement this and it has worked alright. The only problem is that I have to instantiate the players with the PlayerData via the Network, and that means that I can't add the PlayerData to the list on Awake or on Start. If I add it on Awake I get two NullReferenceException errors, one for every Player I guess.
    I have tried adding the PlayerData to the list after spawning but I add only PlayerData of the MasterClient to the list, and not from the other player.