Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

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:
    47
    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:
    8,563
    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:
    47
    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:
    47
    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:
    22
    @Avalin thanks for posting your solution. Agreed, Photon documentation is dreadfully terrible
     
    path14 and Avalin like this.
unityunity