Custom Instantiation 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.

Custom Instantiation Data : Send GameObject or Components ?

Jimanji
2020-03-28 20:13:36

Hello,
its not possible to send GameObject or Components with this technique ? :

object[] myCustomInitData = GetInitData();  
PhotonNetwork.Instantiate("MyPrefabName", new Vector3(0, 0, 0), Quaternion.identity, 0, myCustomInitData);  
Receive custom data:  
public void OnPhotonInstantiate(PhotonMessageInfo info)  
{  
    object[] instantiationData = info.photonView.InstantiationData;  
    // ...  
}  

Thank you

Comments

Tobias
2020-03-30 10:39:21

Right. GameObject and Components are not serializable.
What you can do is create a script for the prefab to enable/disable components or subobjects on demand. Then send a bool[] which ones need to be active.

Jimanji
2020-03-30 12:41:27

Ok thank you, that help me.

DbzRock
2020-11-29 03:29:19

@Tobias how can a make my own object[] myCustomInitData?
I have to pass some int values to the OnPhotonInstantiate but I can't figure out how to do it.

Jimanji
2020-11-29 11:29:43

@DbzRock

int number = 12;  
string word = "hello";

object[] myCustomInitData = new object[]  
          {  
                number,  
                hello  
          };  
        PhotonNetwork.Instantiate(Path.Combine("Players", "Player" , transform.position, Quaternion.identity, 0, myCustomInitData);


public class RandomCall : MonoBehaviourPun, IPunInstantiateMagicCallback  
{  
    public void OnPhotonInstantiate(PhotonMessageInfo info)  
    {  
        object[] instantiationData = info.photonView.InstantiationData;

        int number = (int)instantiationData[0];  
        string word = (string)instantiationData[1];  
    }  
}
Back to top