Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Active/Inactive synchronization (PUN)

Discussion in 'Connected Games' started by II_Spoon_II, Dec 8, 2018 at 2:30 PM.

  1. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    142
    Hello,
    I have an empty Game Object that contains three childs attached to it, I only want one child appearing depending on the Father Gameobject tag, in order to do that, I desactivate the ones I don't want to be visible, which works fine LOCALLY.

    On the other hand the other player can't receive any changes when the tag changes, it only changes on the local machine

    The code is pretty simple (the commented solution is the one I found as an answer of a similar question but it dosen't seem to work
    Code (CSharp):
    1. public class swap : Photon.MonoBehaviour/*, IPunObservable*/ {
    2.     public GameObject blueSon;
    3.     public GameObject redSon;
    4.     public GameObject noneSon;
    5.  
    6.  
    7.    // public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    8.     //{
    9.       //  if (stream.isWriting)
    10.         //{
    11.           //  stream.SendNext(blueSon.activeSelf);
    12.           //   stream.SendNext(redSon.activeSelf);
    13.           //     stream.SendNext(noneSon.activeSelf);
    14.         //}
    15.        // else
    16.        // {
    17.          //   blueSon.SetActive((bool)stream.ReceiveNext());
    18.           //  redSon.SetActive((bool)stream.ReceiveNext());
    19.           //  noneSon.SetActive((bool)stream.ReceiveNext());
    20.         //}
    21.     //}
    22.  
    23.     // Use this for initialization
    24.     void Start () {
    25.      
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update () {
    30.         switch (gameObject.tag)
    31.         {
    32.             case "none":
    33.                 redSon.SetActive(false);
    34.                 blueSon.SetActive(false);
    35.                 noneSon.SetActive(true);
    36.                 break;
    37.  
    38.             case "blue":
    39.                 redSon.SetActive(false);
    40.                 blueSon.SetActive(true);
    41.                 noneSon.SetActive(false);
    42.                 break;
    43.  
    44.             case "red":
    45.                 redSon.SetActive(true);
    46.                 blueSon.SetActive(false);
    47.                 noneSon.SetActive(false);
    48.              
    49.                 break;
    50.         }
    Thanks ^^
     
  2. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    142
  3. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    160
    Something like this could be done on a “as needed” basis.

    Tell your script to change the tag and then let the client know.

    Using PUN2, below should be a good start

    Code (CSharp):
    1. using UnityEngine;
    2. using Photon.Pun;
    3. using Photon.Realtime;
    4.  
    5. public class HelpScript : MonoBehaviourPunCallbacks {
    6.  
    7.     public GameObject blueSon;
    8.     public GameObject redSon;
    9.     public GameObject noneSon;
    10.  
    11.     public byte tagColor;   //0 is none 1 is blue   2 is red
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetKeyDown(KeyCode.Space))
    16.         {
    17.             UpdateTag();
    18.         }
    19.     }
    20.  
    21.     public void UpdateTag()
    22.     {
    23.         if (photonView.IsMine)
    24.             photonView.RPC("UpdateTagMsg", RpcTarget.All, tagColor);
    25.     }
    26.  
    27.     public void UpdateTag(Player newPlayer)
    28.     {
    29.         if (photonView.IsMine)
    30.             photonView.RPC("UpdateTagMsg", newPlayer, tagColor);
    31.     }
    32.  
    33.     [PunRPC]
    34.     void UpdateTagMsg(byte _tagColor)
    35.     {
    36.         switch (_tagColor)
    37.         {
    38.             case 0: //"none":
    39.                 redSon.SetActive(false);
    40.                 blueSon.SetActive(false);
    41.                 noneSon.SetActive(true);
    42.                 break;
    43.  
    44.             case 1:  //"blue":
    45.                 redSon.SetActive(false);
    46.                 blueSon.SetActive(true);
    47.                 noneSon.SetActive(false);
    48.                 break;
    49.  
    50.             case 2: //"red":
    51.                 redSon.SetActive(true);
    52.                 blueSon.SetActive(false);
    53.                 noneSon.SetActive(false);
    54.  
    55.                 break;
    56.         }
    57.     }
    58.  
    59.     public override void OnPlayerEnteredRoom(Player newPlayer)
    60.     {
    61.         UpdateTag(newPlayer);
    62.     }
    63. }
     
    tobiass and Munchy2007 like this.
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    953
    In addition to the solution provided by @voncarp, if you only need this to happen once when the player first enters the game, you could pass the tagColor in the PhotonView.InstantiationData and enable the corresponding gameobject using the OnPhotonInstantiate(PhotonMessageInfo info) callback. The tagColor would be in the info parameter.

    For setting it once only this would be my preferred method.
     
    tobiass likes this.
  5. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    142
    Thank you very much, really appreciate it, but before using the code as it is, can you please explain how the 0,1,2 works? I am not familiar with RPC's (tried using them before but failed). I do understand that you used bytes because RPCs can only send basic data types, but how should I use the bytes in order to change the tags(strings) ? How would I make the 0 as none 1 as blue and 2 as red?
    And since I am using Photon classic, it seems like I cannot make "player" type as a target at line 30, it gives me an error
    Sorry if those are basic questions, but I started doing networking for 2 months I don't understand every single aspect of it, thank you again ^^
     
    Last edited: Dec 10, 2018 at 10:02 PM
  6. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    142
    Thank you for your time ^^ but in my case I will need the tags to change everytime a trigger + a condition happen (not so common but may change every 30-50 secondes, more or less depending on the players tactics )
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    953
    In that case voncarp's method will be better. You could also achieve much the same using a player CustomProperty though, which is probably the way I would handle it.
     
  8. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    142
    Basically the gameObjects I am using this script on are scene objects that exist in the map, not player objects
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    953
    Are you saying you still haven't resolved the issue?

    If they're scene objects (as in objects actually in the scene hierarchy rather than having been instantiated with PhotonNetwork.InstantiateSceneObject() then I'd probably have the MasterClient use RaiseEvent and have a script on them that listens for the event and takes the appropriate action. That way they don't need to have PhotonViews attached to them.

    There's lots of ways to skin a cat and without more detailed information of exactly how you've set things up it's difficult to recommend the best approach. Most likely you should try all of the suggestions made so far and see which works best for you.
     
  10. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    142
    Humm, sorry if I didn't afford enough details. Anyways, to explain more, the objects do exist in the hiearchy, not instantiated, their tags do change when colliding with object that have different tags (I managed that from another script), and when this happens, the activated child should change depends on the new tag.

    If I can provide any other specific information let me know ^^

    I will try with the raiseEevents and see :)
    Thank you.
     
    Munchy2007 likes this.