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

How to show different medals for different players on the same canvas?

Discussion in 'Scripting' started by Jeepster, Oct 11, 2020.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    This may be a tad unusual but I'm making a tournament format multiplayer game and I've made a canvas with images that are filled with gold, silver and bronze medals if a player finishes 1st, 2nd or 3rd.

    The canvas is not a child of any objects and has a don't destroy on load function on it because I need it to carry the placement info and show the medals to each player individually throughout all levels of the tournament.

    If a player finishes 1st, 2nd or 3rd, that player sends an RPC (Photon) if Photonview is Mine - to the canvas telling the canvas which number level and which color medal that player should see: Example :
    Code (CSharp):
    1.  
    2.     [PunRPC]
    3.     void GoldMedalShow3()
    4.     {
    5.         GameObject medalCanv = GameObject.Find("TourMedalCanvas");
    6.         medalCanv.GetComponent<Transform>().GetChild(2).GetComponent<Image>().sprite =      Resources.Load<Sprite>("goldMedal") as Sprite;
    7.     }
    This finds the canvas and assigns a gold medal on the third sprite image, meaning the player won the third level (0 is the first so it's the third on GetChild(2))

    I then RPC it:
    Code (CSharp):
    1.  
    2. GetComponent<PhotonView>().RPC("GoldMedalShow3", PhotonTargets.All);
    3.  
    However ONLY that player should see his medals and not the others and vice versa. This doesn't work with multiple players. Nothing happens.

    I also tried activating the canvas only if photonview is mine and turn if off on else, but that turned off the canvas for both players (all players) with this code:

    Code (CSharp):
    1.  
    2. void Start () {
    3.  
    4.  
    5.     if (photonView.isMine)
    6.        {
    7.          
    8.             GameObject TourManager = GameObject.Find("TourCrownCanvas");
    9.  
    10.             TourManager.SetActive(true);
    11.         }
    12.         else
    13.         {
    14.            
    15.             GameObject TourManager = GameObject.Find("TourMedalCanvas");
    16.  
    17.             TourManager.SetActive(false);
    18.          }
    19. }
    Does anybody know what I'm doing wrong and how I can display what each payer has won individually on a "shared" canvas that is not a child object of any players and is not instantiated since it has to remain active throughout the whole tournament with DontDestroyOnLoad? I want to avoid making separate medal canvases for each player because it would require separate scripts and a lot of re-coding, so that is an absolute last resort. All help is greatly appreciated thanks :)
     
  2. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    It could be possible that you are checking photonView.mine before it could be instantiated. So try putting it in Update() inside a if condition.

    There is another alternative but would add to your coding efforts.
    You can try providing IDs to each player. And while sending a message from server ensure to send the player-id . If the player-id sent by server and the client's id is same, TourManager.SetActive(true) . If not matching then TourManager.SetActive(false)
     
    Jeepster likes this.
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Awesome idea with the ID's. I'll try it out thank you for your help