Search Unity

TagObject of previous clients is null for new clients

Discussion in 'Connected Games' started by Avalin, Oct 19, 2018.

  1. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    38
    I finally managed to send CustomProperties to server! Yay!
    This means every new client gets a color, and all previous clients can see that color as hoped...
    However, new clients can't see previous clients color - because their TagObject returns null.
    I have this code:
    Code (CSharp):
    1.     void IPunInstantiateMagicCallback.OnPhotonInstantiate(PhotonMessageInfo info)
    2.     {
    3.         info.Sender.TagObject = this.gameObject;
    4.     }
    In the object I want each player to have as TagObject I also added this to the start of my gameObject:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         pv = GetComponent<PhotonView>();
    4.         pv.Owner.TagObject = this.gameObject;
    5.     }
    And here's my custom property update-code:

    Code (CSharp):
    1.     void IInRoomCallbacks.OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
    2.     {
    3.  
    4.         GameObject targetGO = (GameObject)targetPlayer.TagObject;
    5.  
    6.         object colorR = targetPlayer.CustomProperties["colorRval"];
    7.         object colorG = targetPlayer.CustomProperties["colorGval"];
    8.         object colorB = targetPlayer.CustomProperties["colorBval"];
    9.  
    10.         if (colorR != null && colorG != null & colorB != null)
    11.         {
    12.             float colorRval = float.Parse(colorR.ToString());
    13.             float colorGval = float.Parse(colorG.ToString());
    14.             float colorBval = float.Parse(colorB.ToString());
    15.  
    16.             Color targetColor = new Color(colorRval, colorGval, colorBval);
    17.  
    18.  
    19.             if (targetPlayer.TagObject == null)
    20.             {
    21.                 Debug.Log(targetPlayer + " has no gameobject. Should NEVER happen");
    22.             }
    23.             else
    24.             {
    25.                 targetGO.GetComponent<SpriteRenderer>().color = targetColor;
    26.             }
    27.         }
    28.     }
    The first client never has any errors, but in any new client everyone who logged in before themselves do not get the color change, but instead the debug message.

    Will I need to store the TagObjects locally or am I calling it in a wrong order, etc.?
     
    Last edited: Oct 19, 2018
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,049
    You should mention in your thread title this is regarding Photon, or the people knowledgeable with Photon won't necessarily show up.
     
  3. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    38
    Ah yes, thanks for the advice! The edit function doesn't seem to provide me with a way to change the name though. :( Can't even add the tag "Photon". :/
     
  4. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    38
    To anyone running into the same problem, I managed to fix it.
    I wrapped everything inside the OnPlayerPropertiesUpdate in a foreach-loop checking every photonView in my scene, and then checking if any of them has an owner. If they do, instead of getting a targetplayers customProperties, I got the respective photonview's owner's custom property and set it that way. It's probably a very bad solution, but with absolutely no information, tutorials or anything to go by, this is the best I could come up with
     
  5. Moonstorm

    Moonstorm

    Joined:
    Jul 26, 2012
    Posts:
    21
    @Avalin thanks for posting your solution. Agreed, Photon documentation is dreadfully terrible
     
    Avalin likes this.