[TargetRPC] is causing a NullReferenceException... HELP!

So I updated to the 5.4 beta. I want to test the new [TargetRPC] with a simple “Hello world!” script:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class RampUp : NetworkBehaviour {

    void OnTriggerEnter(Collider target){
        if (target.tag == "Hand_NoTeam" || target.tag == "Hand_Green" || target.tag == "Hand_Orange" || target.tag == "Hand_Blue") {         
            CmdRampUp();     
        }
    }

    [TargetRpc]
    public void TargetRampUp(NetworkConnection target){
        Debug.Log ("Cheeseburger!");
    }

    [Command]
    void CmdRampUp (){
        TargetRampUp (connectionToClient);
    }
}

The result:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkBehaviour.SendTargetRPCInternal (UnityEngine.Networking.NetworkConnection conn, UnityEngine.Networking.NetworkWriter writer, Int32 channelId, System.String rpcName) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkBehaviour.cs:122)
RampUp.CallTargetRampUp (UnityEngine.Networking.NetworkConnection target)
RampUp.CmdRampUp () (at Assets/Scripts/Vibration/RampUp.cs:20)
RampUp.CallCmdRampUp ()
RampUp.OnTriggerEnter (UnityEngine.Collider target) (at Assets/Scripts/Vibration/RampUp.cs:9)

I’m really new to all the netcode stuff, can somebody explain that NullRefeferenceException to me?

Have you spawned your object from the server? If not there server has no idea who to send the RPC call to.

You can take a look here for more information about spawning and actions.

I’m using the NetworkManger + NetworkManagerHUD to set up everything. Aren’t those doing that for me by default?

So far everything is working. Changing certain components for example, like the material of each Client works just fine with this method:

[ClientRpc]
    void RpcTag_NoTeam(GameObject player){
        player.GetComponent<SpriteRenderer> ().sprite  = noTeam;
        player.GetComponent<LineRenderer> ().material = noTeam_arm;
        player.tag = "Team_NoTeam";
        player.gameObject.transform.GetChild (0).gameObject.tag = "Hand_NoTeam";
        player.gameObject.transform.GetChild (0).gameObject.GetComponent<SpriteRenderer> ().sprite = noTeam_nucleus;
    }

    [Command]
    public void CmdTag_NoTeam(GameObject player) {
        RpcTag_NoTeam (player); // Use RPC function to change attriutes
    }

I just can’t figure out how to identify single clients.

You need to setup what the server should spawn as a player on startup. This is done by adding your player prefab under the Spawn Info tab in the Network Manager Component (see the picture).

Then when you updated your prefab remove it from the scene as it will be spawned by the NetworkServer.

2698254--191099--addprefab.png

Right, that’s what I’m doing. I can play with multiple clients on the server and they do what they should so far. They are spawned by the NetworkManager as PlayerPrefab.

The problem seems to be, that …

print (connectionToClient);

… is returning Null.

Thanks for your help so far, I guess with this proect I bit of kind of a piece that’s al little to hard to chew :slight_smile:

/Edit Sorry, my mistake was, that I tried to access the client through a child of the PlayerPrefab. It should work now, if I can figure out how to get the connectionToClient of the parent

/Edit#2 It works ! :slight_smile:

1 Like