PUN 2, OnPhotonInstantiate isn't being called

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.

PUN 2, OnPhotonInstantiate isn't being called

Vallar
2018-09-18 06:52:12

Hi all,

I was wondering how exactly do I get OnPhotonInstantiate to be called correctly?

What I have right now is a script inheriting from MonoBehaviourPunCallbacks and is subscribed using PhotonNetwork.AddCallbackTarget(this); in an overrided OnEnable as the migration guide suggests. However it isn't being called when a player is being instantiated.

Is there something I am missing?

Thanks.

Comments

[Deleted User]
2018-09-18 10:27:38

Hi @Vallar,

deriving from MonoBehaviourPunCallbacks is not enough in this case. You would have to implement the IPunInstantiateMagicCallback as well. Having implemented this, the OnPhotonInstantiate function gets called.

Vallar
2018-09-18 12:05:34

@Christian_Simon wrote:

Hi @Vallar,

deriving from MonoBehaviourPunCallbacks is not enough in this case. You would have to implement the IPunInstantiateMagicCallback as well. Having implemented this, the OnPhotonInstantiate function gets called.

Oh, I see. Thanks @Christian_Simon I thought implementing that would be enough as other callbacks appeared. Will implement the other one. .

[Deleted User]
2018-09-18 12:16:18

IPunInstantiateMagicCallback is different from the other callbacks, because you don't have to register it. Therefore it is not implemented in MonoBehaviourPunCallbacks like other interfaces, for example IConnectionCallbacks or IInRoomCallbacks.

Alex_P
2018-10-03 13:51:05

@Christian_Simon wrote:

Hi @Vallar,

You would have to implement the IPunInstantiateMagicCallback as well. Having implemented this, the OnPhotonInstantiate function gets called.

Could you please clarify exactly how one would go about implementing IPunInstantiateMagicCallback? I'm sure I'm just missing some basic unity knowledge about callbacks here, but I can't find info anywhere on how you actually would do that in code!

Alex_P
2018-10-03 14:30:17

Nevermind, I found a unity tutorial about interfaces and found my answer. For anyone else in my situation...

  
using UnityEngine;  
using Photon.Pun;

public class MyScriptName: MonoBehaviour, IPunInstantiateMagicCallback  
{  
	public void OnPhotonInstantiate(Photon.Pun.PhotonMessageInfo info)	  
	{  
		// Example...   
		Debug.Log("Is this mine?... "+info.Sender.IsLocal.ToString());  
	}  
}

ConceptFactory
2019-05-09 19:53:43

Still not working for me

jrDev
2019-09-25 03:46:31

@Alex_P wrote:

Nevermind, I found a unity tutorial about interfaces and found my answer. For anyone else in my situation...

  
using UnityEngine;  
using Photon.Pun;

public class MyScriptName: MonoBehaviour, IPunInstantiateMagicCallback  
{  
  public void OnPhotonInstantiate(Photon.Pun.PhotonMessageInfo info)	  
  {  
  	// Example...   
  	Debug.Log("Is this mine?... "+info.Sender.IsLocal.ToString());  
  }  
}

Does this script go on the actual gameobject that's instantiated or any gameobject?

EDIT: It looks like it. The PlaymakerGameObjectProxy script has it which needs to be attached to any instantiated object.

But if can only be triggered on that specific gameobject, is there a point to use OnPhotonInstantiate instead of the default Start Class/Event?

Thanks,
jrDev

Anopey
2019-09-25 07:48:09

You can send custom instantiation info to all clients that instantiate said object.

Back to top