Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

clientscene.unregisterPrefab dosen't work?

Discussion in 'Multiplayer' started by khoucem, Oct 7, 2017.

  1. khoucem

    khoucem

    Joined:
    Feb 15, 2017
    Posts:
    1
    Hi,
    I'm creating a card game , what i do need now is to distribute the cards on the players on the network here is the script that i developed which is attached to the player prefab :

    Code (CSharp):
    1. [System.Serializable]
    2. public class ToggleEvent: UnityEvent <bool>{}
    3.  
    4. public class Player : NetworkBehaviour {
    5.  
    6.  
    7. [SerializeField] ToggleEvent onToggleShared;
    8. [SerializeField] ToggleEvent onToggleLocal;
    9. [SerializeField] ToggleEvent onToggleRemote;
    10.  
    11.  
    12.  
    13. List<GameObject> Cards,EightCardsReceived ;
    14. List<Transform> startPositionsTransformList;
    15. List<Vector2> startPositionsList;
    16.  
    17.  
    18.  
    19.  
    20. public override void OnStartServer(){
    21.    distributeCards ();
    22.  
    23.  
    24. }
    25. IEnumerator Mycoroutine (){
    26.    yield return new WaitForSeconds(10f);
    27.    Debug.Log (ClientScene.ready+"  Clientscene ready");
    28.    if (ClientScene.ready)
    29.        RpcdistributeCards ();
    30. }
    31.  
    32. [Server]
    33. void distributeCards(){
    34.    StartCoroutine ("Mycoroutine");
    35.  
    36.  
    37. }
    38. [ClientRpc]
    39. void RpcdistributeCards(){
    40.  
    41.    EightCardsReceived = PickUpCards ();
    42.  
    43.    if (isLocalPlayer) {
    44.  
    45.        for (int j = 0; j < 8; j++) {
    46.  
    47.            Vector2 cardPosition = positionCalculator (j);
    48.            Instantiate (EightCardsReceived [j], cardPosition, Quaternion.identity);
    49.        }
    50.    }
    51. }
    52.  
    53. void Start()
    54. {
    55.    EnablePlayer ();
    56.  
    57.  
    58. }
    59.  
    60. void DisablePlayer()
    61. {
    62.    onToggleShared.Invoke (false);
    63.    if (isLocalPlayer)
    64.        onToggleLocal.Invoke (false);
    65.    else
    66.        onToggleRemote.Invoke (false);
    67.  
    68. }
    69.  
    70. void EnablePlayer()
    71. {
    72.    onToggleShared.Invoke (true);
    73.    if (isLocalPlayer)
    74.        onToggleLocal.Invoke (true);
    75.    else
    76.        onToggleRemote.Invoke (true);
    77. }
    78.  
    79. Vector2 positionCalculator (int J){
    80.    Vector2 finalPosition;
    81.    Vector2 currentStartPosition = gameObject.transform.position;
    82.    Vector2 Increment = new Vector2 (1.41f, 0f);
    83.    finalPosition = currentStartPosition + J * Increment;
    84.  
    85.    return finalPosition;
    86.  
    87.  
    88. }
    89. List<GameObject> PickUpCards (){
    90.    List<GameObject> EightCards = new List<GameObject>();
    91.    NetworkManager netM = NetworkManager.singleton;
    92.    for (int i = 0; i < 8; i++) {
    93.        GameObject currentGameObject = netM.spawnPrefabs [Random.Range (0, netM.spawnPrefabs.Count)];
    94.        ClientScene.UnregisterPrefab (currentGameObject);
    95.        Debug.Log (i);
    96.        Debug.Log (netM.spawnPrefabs.Count);
    97.        EightCards.Add (currentGameObject);
    98.  
    99.    }
    100.    return EightCards;
    101. }
    102. }
    In the console i get that the count of registered spawnable prefab dosen't decrease so clientscene.unregisterPrefab isn't working.

    I appreciate any help . Thanks for reading.