The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

Try Our

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

TagObject usage?

2015-08-04 22:36:32

I have a problem and I think TagObject might be my solution. I cannot find any documentation anywhere on how to use it. If someone could write up a quick usage tutorial, I would be grateful.

However, if it ends up not being my solution, I will describe my problem and am asking for advice.

I need to access the gameobject of the player that created an object. The object is a UI button, and when the player on another client clicks it, it needs to call methods and change variables on the original player, the one that instantiated it. I was thinking I could access the player through the photonview somehow. How can I access a player's "player" gameobject through an object that they own that is not in the same hierarchy? I need to call a function back on the original player based on a different player's interactions with the object.

(Sorry if I haven't explained myself well. If there is some part that I need to explain in more detail, please ask me)


2015-08-05 11:45:26

The TagObject is a object variable that can be used by your code any way you like. It's not synchronized or anything but each client can store something player-related in there.

Let's say each player has a (single) character. The character gets instantiated via PhotonNetwork.Instantiate and on the prefab, a script implements OnPhotonInstantiate(PhotonMessageInfo info). This is called when the object gets created and the info variable contains who instantiated it. So you can store this.gameObject as PhotonPlayer.
The benefit: If any code provides you with a PhotonPlayer, you have the reference to the GameObject which is that player's character.


2015-08-06 01:06:12

Thank you Tobias. This worked as I was hoping. When I get a chance, I will come back to this thread and post an explanation and my solution for others who have a similar situation.

2017-04-10 13:57:18

This Link is no longer working and I would love access to it.

public class ScriptAttachedToYourPrefabPlayer : PunBehaviour
public override void OnPhotonInstantiate(PhotonMessageInfo info)
//Assign this gameObject to player called instantiate the prefab
info.sender.TagObject = this.gameObject;

This works great but

public override void OnPhotonPlayerDisconnected( PhotonPlayer playerWhoLeft )
Debug.Log( "OnPhotonPlayerDisconnected() " + playerWhoLeft.TagObject);

my debug log shows my master client object. I'm sure who info.sender.TagObject ends up being stored in PhotonPlayer.TagObject nor why it seemed to store the wrong object for me.

Note: I'm trying to access a gameObject right before it is destroyed when a player kills the game unexpectedly (to clean up scene objects that are not children but that are being affected by the player gameObject.)

2017-04-10 14:55:42

Edit.. Debug.Log( "OnPhotonPlayerDisconnected() " + playerWhoLeft.TagObject);
does appear to be storing the name of the gameObject who disconnected.

I'm just not sure how or where this happened: Playerobject.TagObject = Infomessage.sender.TagObject

[Deleted User]
2017-04-10 15:08:20

Hi @nomika,

the Instantiation doc is still available, you can find it over here.

TagObject actually stores a reference to a game object. When you use this game object in Debug.Log, its ToString() function gets automatically called and returns its name. Since names are always the same when being instantiated using PhotonNetwork.Instiatate - regardless of which player instantiated which object and non of these objects gets renamed - you can not say if "Prefab (Clone)" is actually your prefab or someone's other.

Back to top