photonView.RPC works for some components, but not for others

I have a component/class called InventoryController attached to any object in my game which needs an inventory of some sort. And things works just fine; it derives from Photon.MonoBehaviour, it includes several RPCs to keep things in sync over the network, and everything is fantastic, ie;

[code2=csharp]public class InventoryController : Photon.MonoBehaviour {

[RPC]
private void RPC_AddItems ( string itemName, int amount ) {
this.AddItems( itemName, amount );
}

public DoSomething () {
photonView.RPC( "RPC_AddItems", ... );
}

}[/code2]

Because this was working just fine, I thought it would work fine if I used the same approach to other objects in my game, like a rock. I attach a RockController to it, make it derive from Photon.MonoBehaviour, and I thought I could do the same photonView.RPC magic out of the box like with the InventoryController.

But I can't!

In this component, photonView is always null. Why is that?

Comments

  • Do rock and other objects have PhotonView attached?
    If you set 'photonView' properly during initialization then null means no PhotonView on object.
  • That's what really puzzles me, because the InventoryController does NOT have a PhotonView attached to it. And how could it, be it that it's a component, not a prefab/game object? So I was hoping that "Photon.MonoBehaviour" helped out, not needing to attach a PhotonView "everywhere".

    But I guess I'm wrong... :)
  • You need PhotonView component to send RPC's.
    Inheriting Photon.MonoBehaviour just gives you 'photonView' property which returns PhotonView component of the object (so I was wrong stating that you need to initialize it). Without PhotonView attached this property will be always null.
    InventoryController works for you only when attached to object with PhotonView.
  • Aha! So if the InventoryController is attached to a game object that has a PhotonView, it will automagically "get it" as long as it derives from Photon.MonoBehaviour? If so, things are starting to make sense.

    However, I still feel a bit uncomfortable having PhotonViews "everywhere", in this case it's needed for a GUI panel to work as expected (which I just confirmed.) So the next question would be:

    Does it make sense - and is it a good thing - to have a game object called "RPC" in the scene, attach a PhotonView to it, attach a singleton class to it, and use that for all (...) RPC-calls in the game, ie. something like this?

    [code2=csharp]public class RPCController : Photon.MonoBehaviour {

    static public PlayerController instance;

    void Start () {
    instance = this;
    }

    [RPC]
    public SomethingNetworky () {
    // ...
    }

    }[/code2]
    Then somewhere totally elsewhere:

    [code2=csharp]public class FoobarController : MonoBehaviour {

    void DoSomething () {
    RPCController.instance.SomethingNetworky();
    }

    }[/code2]

    ...or is this plain stupid, if it even works?