Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Help with Multiplayer random Color generation?

Discussion in 'Multiplayer' started by Here2ChewGum, Jun 19, 2023.

  1. Here2ChewGum

    Here2ChewGum

    Joined:
    Jan 23, 2023
    Posts:
    2
    Hi all,

    Extremely new to unity and am a beginner so sorry if this is a stupid question, just looking for help as struggling. Making a multiplayer game with Photon in which I would like to randomly assign player prefabs with different colours on instantiation. I have been able to make it work so that they are randomly generated from a list of 10 colors with the following code, however I am wondering if there is a way to make it so that the same color is not repeated. Any help anyone could provide would be greatly appreciated.
    Code (CSharp):
    1. using System.Collections;
    2. using Photon.Pun;
    3. using Photon.Realtime;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.SceneManagement;
    7. using UnityEngine.UI;
    8.  
    9. public class PlayerInfo :
    10. Photon.Pun.MonoBehaviourPun, IPunObservable
    11. {
    12.  
    13.     public int colorIndex;
    14.     public SpriteRenderer playerBody;
    15.  
    16.     public List<Color> _allPlayerColors = new List<Color>();
    17.     public Text _playerName;
    18.  
    19.     public Color CurrentColor
    20.     {
    21.         get
    22.         { return _allPlayerColors[colorIndex]; }
    23.     }
    24.  
    25.     private void Awake()
    26.     {
    27.         if (photonView.IsMine)
    28.         {
    29.             colorIndex = Random.Range(0, _allPlayerColors.Count);
    30.             _playerName.text = PhotonNetwork.LocalPlayer.NickName;
    31.         }
    32.         else
    33.         {
    34.             _playerName.text = GetPlayerName(photonView.OwnerActorNr);
    35.         }
    36.     }
    37.  
    38.     public void OnPhotonSerializeView(PhotonStream stream,
    39.              PhotonMessageInfo info)
    40.     {
    41.  
    42.         if (stream.IsWriting)
    43.         {
    44.             // Owner.
    45.             stream.SendNext(colorIndex);
    46.         }
    47.         else
    48.         {
    49.             // Remote.
    50.             colorIndex = (int)stream.ReceiveNext();
    51.         }
    52.     }
    53.     private void Update()
    54.     {
    55.         playerBody.color = _allPlayerColors[colorIndex];
    56.     }
    57.     private string GetPlayerName(int actorID)
    58.     {
    59.         foreach (var player in PhotonNetwork.CurrentRoom.Players)
    60.         {
    61.             if (player.Value.ActorNumber == actorID)
    62.             {
    63.                 return player.Value.NickName;
    64.             }
    65.         }
    66.         return "[none]";
    67.     }
    68. }
     
  2. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    77
    You can use something like
    PhotonNetwork.LocalPlayer.ActorNumber
    as the index for chosing the color. That way each player gets a different color. This works for simple uses cases but not if players disconnect / reconnect at runtime. If you want to solve that and support that as well you'd need to do something like storing the already taken colors in a room property or a PhotonView.
     
  3. Here2ChewGum

    Here2ChewGum

    Joined:
    Jan 23, 2023
    Posts:
    2
    Hi, thank you for the response. Sorry but I am a bit unclear as to how I would incorporate that into this code?