RPC Null reference

Options
Hey, just trying to get my head around RPC's, as i havent done any networking before.

from what i have read in the doc's if you put [RPC] above the method you want to call, the photonview.RPC function will do the same thing for the requested targets.

so i have my code setup like this:

[code2=csharp]bool chatStory = true;

void OnClick(){
if(enabled){
if(chatStory == true){
PhotonView photonView = PhotonView.Get(this);
photonView.RPC("AddChat",PhotonTargets.All);
}
}
}

[RPC]
void AddChat(){
Debug.Log("rpc test working");
}[/code2]

(don't worry about the chatStory bool, it is for another part in my game)

when i run this code however i get this:
NullReferenceException: Object reference not set to an instance of an object

isn't the instanced object this script? I don't understand why i get this.

--EDIT--

sorry i added a photonview to the ngui button that i have this script attached. i also attached the enterbutton script to the "Observe" variable area in the inspector. now i get two errors:

Illegal view ID:0 method: AddChat GO:EnterButton, and
Received RPC "AddChat" for viewID 0 but this PhotonView does not exist!

any help is great thanks.

Comments

  • 2Kin
    Options
    You have to instantiate your object over the network with PhotonNetwork.Instantiate, or use PhotonNetwork.AllocateViewID() and set it to your photonView.viewID.
    If you don't, your PhotonView won't be known by the server.
  • 2Kin wrote:
    You have to instantiate your object over the network with PhotonNetwork.Instantiate, or use PhotonNetwork.AllocateViewID() and set it to your photonView.viewID.
    If you don't, your PhotonView won't be known by the server.

    That did the trick, thanks heaps for the help. :D
  • SaravanaKumar
    Options
    I am also getting same error now. But, you from the above answer says We should create prefab to communicate or else use PhotonNetowrk.AllocateViewID();

    I tried Allocating ID.

    Just tell the i am doing it in the right way.
    Here is how i allocated.

    [code2=csharp]PhotonView networkView = PhotonView.Get(this);
    networkView.viewID = PhotonNetwork.AllocateViewID();
    networkView.RPC("ChatMessage", PhotonTargets.All);

    [RPC]
    void ChatMessage()
    {
    Debug.Log("Chatting Started");
    }[/code2]

    But, still its showing NullReferenceException: Object reference not set to an instance of an object
  • Tobias
    Options
    Please put a GameObject with PhotonView in your scene and use that to communicate or instantiate a prefab with PhotonView.
    The included PDF document has a brief intro to allocating viewIDs yourself but that's not the preferred and supported way.