How to use PhotonNetwork.Instantiate with Object Data

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

  • 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];
    
  • 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 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'
  • PhotonView.instantiationData tries to access instantiationData as static member of PhotonView but that's not static.
    Access "this game object's" .instantiationData.
  • 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?
  • 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
    develax ✭✭
    edited June 2019
    Tobias said:

    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.