PUN 2, OnPhotonInstantiate isn't being called

Options
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

  • 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
    Options

    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. .
  • 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
    Options

    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
    Options
    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
    Options
    Still not working for me
  • jrDev
    jrDev
    edited September 2019
    Options
    Alex_P said:

    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
    Options
    You can send custom instantiation info to all clients that instantiate said object.