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

bool value doesn't update

Discussion in 'Multiplayer' started by Slack43, Feb 26, 2018.

  1. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    So i have this code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class ClassManager : NetworkBehaviour {
    7.     [SerializeField] Color seekerClassColor;
    8.     [SerializeField] Color hiderClassColor;
    9.  
    10.     [SerializeField] bool isApplied = false;
    11.  
    12.     public Material playerMaterial;
    13.  
    14.     PlayerClass player;
    15.     ServerVariables svar;
    16.  
    17.     void Start()
    18.     {
    19.         player = this.GetComponent<PlayerClass>();
    20.         svar = FindObjectOfType<ServerVariables>();
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         if (Input.GetKeyDown(KeyCode.F))
    26.         {
    27.             RpcSetClass();
    28.         }
    29.     }
    30.  
    31.     void OnPlayerConnected(NetworkPlayer player)
    32.     {
    33.         RpcSetClass();
    34.     }
    35.  
    36.     [ClientRpc]
    37.     public void RpcSetClass()
    38.     {
    39.         player.RpcRandomizeClass();
    40.        if(isApplied == false)
    41.         {
    42.             if (player.ClassID == 1)
    43.             {
    44.                 if (svar.Seekers <= 2)
    45.                 {
    46.                     RpcSetAttributes(true);
    47.                 }
    48.             }
    49.             if(player.ClassID == 2)
    50.             {
    51.                 if(svar.Hiders <= 2)
    52.                 {
    53.                     RpcSetAttributes(false);
    54.                 }
    55.             }
    56.         }
    57.     }
    58.     [ClientRpc]
    59.     void RpcSetAttributes(bool toSeeker)
    60.     {
    61.         if (toSeeker)
    62.         {
    63.             playerMaterial.color = seekerClassColor;
    64.             svar.CmdAddPlayer(true);
    65.             isApplied = true;
    66.             GetComponent<Seeker>().enabled = true;
    67.             this.tag = "Seeker";
    68.         }
    69.         else
    70.         {
    71.             playerMaterial.color = hiderClassColor;
    72.             svar.CmdAddPlayer(false);
    73.             isApplied = true;
    74.             GetComponent<Seeker>().enabled = false;
    75.             this.tag = "Hider";
    76.         }
    77.     }
    78.  
    79.  
    80.  
    81.  
    82.  
    83. }
    The isApplied value is always false.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't try to call a ClientRpc from a client, which is what you're doing when you call RpcSetAttributes inside RpcSetClass. If you're only calling RpcSetAttributes from the client then just make it a regular method called by RpcSetClass.
     
  3. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    True, i think i made it an rpc because i wanted it to be universal. Thanks. I have one more question, i've heard that host is a server and client at same time, so can i make [Commands] work on host?
     
  4. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    Take a look at this class
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerClass : NetworkBehaviour {
    7.     [SerializeField]private Camera playerCamera;
    8.     [SerializeField]private Camera spectatorCamera;
    9.  
    10.     //ID 1 = Seeker | ID 2 = Hider
    11.     [SyncVar]public int ClassID;
    12.  
    13.     //void Start()
    14.     //{
    15.     //    RpcRandomizeClass();
    16.     //}
    17.  
    18.     [Command]
    19.     public void CmdSetPlayer()
    20.     {
    21.         spectatorCamera.gameObject.SetActive(false);
    22.         playerCamera.gameObject.SetActive(true);
    23.     }
    24.     [Command]
    25.     public void CmdSetSpectator()
    26.     {
    27.         spectatorCamera.gameObject.SetActive(true);
    28.         playerCamera.gameObject.SetActive(false);
    29.     }
    30.     [Command]
    31.     public void CmdRandomizeClass()
    32.     {
    33.         ClassID = Random.Range(1, 3);
    34.     }
    35. }
    36.  
    When i press "F" the isApplied is true but when im using SetAttributes() it doesn't change. Why the ClassID anytime on start is equal to 0?
     
  5. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    Ok, i fixed the problem: The
    1. void OnPlayerConnected(NetworkPlayer player)
    2. {
    3. }
    4. Isn't working properly, well it isn't working at all, and one more thing. Error in even other class which isn't even using any methods in your class can affect entire code to crash and don't work properly.