Search Unity

Question How to make separate canvases with netcode

Discussion in 'Netcode for GameObjects' started by XeLLLeR, May 10, 2022.

  1. XeLLLeR

    XeLLLeR

    Joined:
    Dec 5, 2020
    Posts:
    30
    I have a player prefab that contains canvas as a child object. In this canvas exist two buttons - RotateBody and Fire. Depending on the role that player receive I need to enable concrete button. For example I have two players, first player has Seeker role while second has Hider role. In first case I need to enable only Fire button in canvas and RotateBody button in second case. But canvases overlap each other somehow. I tried to disable canvas object and components of canvas if it's not a localPlayer, but still have the issue. So can you give me some tips how can I solve this problem?
     
  2. edin97

    edin97

    Joined:
    Aug 9, 2021
    Posts:
    58
    If I understood correctly, the way I do this is :

    I disable both Canvas on the prefab, and when the player spawns I enable the correct canvas for the player. So, first of all, you need a way to differentiate the Seeker and the Hider. Maybe with a Tag or creating teams but that's a bit more complicated. If you don't have that, this is the way to avoid canvas overlapping : in one of your script you need to add this :

    Code (CSharp):
    1.    
    2.     [SerializeField] private GameObject Canvas_HUD;
    3.  
    4. //OnNetworkSpawn is called when the player spawns on the server
    5.     public override void OnNetworkSpawn()
    6.     {
    7.         base.OnNetworkSpawn();
    8.         // Make sure this belongs to us, so canvas don't overlap
    9.         if (!IsOwner) { return; }
    10.         // the canvas you want to enable
    11.         Canvas_HUD.SetActive(true);
    12.     }
    13.  
    But If you have a way to differentiate the Seeker and the Hider, in the same code you can add it to enable the correct canvas, something like this :

    Code (CSharp):
    1.    
    2. [SerializeField] private GameObject Canvas_Seeker;
    3. [SerializeField] private GameObject Canvas_Hider;
    4. //OnNetworkSpawn is called when the player spawns on the server
    5.     public override void OnNetworkSpawn()
    6.     {
    7.         base.OnNetworkSpawn();
    8.         // Make sure this belongs to us, so canvas don't overlap
    9.         if (!IsOwner) { return; }
    10.         // if the player is a seeker
    11.         if (IsSeeker)
    12.         {
    13.             //Seeker's canvas
    14.             Canvas_Seeker.SetActive(true);
    15.         }
    16.         // if the player is a hider
    17.         else if (IsHider)
    18.        {
    19.             //Hider's canvas
    20.             Canvas_Hider.SetActive(true);
    21.        }
    22.     }
    23.  
    hopefully that's what you're looking for
     
  3. XeLLLeR

    XeLLLeR

    Joined:
    Dec 5, 2020
    Posts:
    30
    It works perfectly! Thank you, lifesaver! I just made two different prefabs one for each role. And put your suggestion into the code and now it works. Big thanks!
     
    edin97 likes this.
  4. edin97

    edin97

    Joined:
    Aug 9, 2021
    Posts:
    58
    No problem, glad I could help ! :)