Search Unity

NetworkObject not spawning on UI for clients

Discussion in 'Netcode for GameObjects' started by CptMarvel21, May 15, 2022.

  1. CptMarvel21

    CptMarvel21

    Joined:
    Sep 18, 2021
    Posts:
    12
    My plan is to spawn a NetworkObject, which is a card, into a UI canvas called Hand. After Spawning the card, it only shows on the Host UI and not the Clients'. I then tried putting a NetworkObject Component on the Hand UI to see if that was the problem, but the client still doesnt see the cards, and it also says only the server can change parents.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Netcode;
    5.  
    6. public class PlayerManager : NetworkBehaviour
    7. {
    8.     public NetworkObject cardPrefab;
    9.     public RectTransform handTransform;
    10.    
    11.  
    12.     public override void OnNetworkSpawn()
    13.     {
    14.         handTransform = GameObject.Find("Hand").GetComponent<RectTransform>();
    15.     }
    16.  
    17.     public void Update()
    18.     {
    19.         if(!IsOwner) { return; }
    20.         if (!Input.GetKeyDown(KeyCode.Space)) { return; }
    21.  
    22.         SpawnCardServerRpc();
    23.     }
    24.    
    25.     [ServerRpc]
    26.     public void SpawnCardServerRpc()
    27.     {
    28.         NetworkObject networkCard = Instantiate(cardPrefab, handTransform);
    29.         networkCard.SpawnWithOwnership(OwnerClientId);
    30.     }
    31. }
    32.