How to use PhotonNetwork.Instantiate with Object Data

The whole answer can be found below.

Please note: 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! And we offer you support through these channels:

Try Our
Documentation

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.

Read More on
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.

How to use PhotonNetwork.Instantiate with Object Data

thetnswe
2012-04-03 15:53:58

public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)

How can I use this to also contains the custom data for PhotonObject? I created my bullet which contains PhotonView and I need to set some properties of my bullet so it is sync with all the clients. How can I get the data back from other client? It's quite easy to get the data for RPC calls but I have no idea of how can I set custom data with Instantiate.. :)

I really appreciate for any helps..

Comments

thetnswe
2012-04-04 02:23:20

I Found a solution to myself.. I can assign data using the following code

object[] data = new object[2];
                    data[0] = range;
                    data[1] = number;

                    GameObject objMeteor = (GameObject)PhotonNetwork.Instantiate("Network_MeteorStrike", tankController.RayHitPoint + new Vector3(0.0f, 100.0f, 0.0f), Quaternion.identity, 0, data);
                    

and retrieve the instantiate data using the following .. :)

        object[] data = photonView.instantiationData;
        radius = (float)data[0];
        number = (int)data[1];

Tobias
2012-04-11 12:30:54

Good job :)

By the way: bullets with PhotonView? This doesn't sound like a good idea. A bullet is something that doesn't really need any position syncing or it's own PRCs. Also, there could be a lot of those. This will slow down your game. You might want to change this. Maybe use RPCs instead of instantiate a bullet and then destroy it very soon.

peaveyyyy
2014-07-23 10:20:56

I can't seem to use this correctly...

When I use:

object[] data = PhotonView.instantiationData ;

I get a compiler error when building:

An object reference is required for the non-static filed, method or property 'PhotonView.instantiationData.get'

Tobias
2014-07-23 11:35:46

PhotonView.instantiationData tries to access instantiationData as static member of PhotonView but that's not static. Access "this game object's" .instantiationData.

peaveyyyy
2014-07-23 13:19:27

Thanks, that's great. To clarify for other users....

object[] data = this.gameObject.GetPhotonView ().instantiationData;

EDIT

The Instantiate method works if I use simple strings for the object array.

If I try to pass an actual object with any complexity, it fails silently. Is there a restriction on the object type that may be passed in the data variable?

Epic
2017-04-20 15:16:09

I was finding this syntax very weird with the &# symbols etc and did not get it at first.

I've posted a simpler solution for noobs like me here:
https://forum.unity3d.com/threads/photon-unity-networking.101734/page-30#post-3037454

develax
2019-06-30 17:35:16

@Tobias wrote:

Good job :)



By the way: bullets with PhotonView?

This doesn't sound like a good idea. A bullet is something that doesn't really need any position syncing or it's own PRCs. Also, there could be a lot of those. This will slow down your game.

You might want to change this. Maybe use RPCs instead of instantiate a bullet and then destroy it very soon.

I guess it depends on what kind of bullets are used. If the bullet flies slowly enough to be visible it will not be good if it flies past the object and hits it at the same time.

Back to top