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
Just an idea…
static class with some data.
public static class GameManager
{
public static List<PlayerData> playersData = new List<PlayerData>();
}
At PlayerData, attached to each player:
public class PlayerData
{
public string playerName;
public float/int score;
void Awake()
{
GameManager.playersData.Add(this);
}
}
At spheres:
public class Sphere
{
void OnTriggerEnter2D(Collider2D col)
{
if (col.transform.CompareTag("Player"))
col.gameObject.GetComponent<PlayerData>().score++;
Destroy(gameObject);
}
}
U can get all your player scores with some like:
foreach(PlayerData playerData in GameManager.playersData)
{
print(playerData.playerName + ": " + playerData.score.ToString());
}
2 Likes
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.