Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

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

Discussion in '5.4 Beta' started by Di_o, Jun 27, 2016.

  1. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62
    So I updated to the 5.4 beta. I want to test the new [TargetRPC] with a simple "Hello world!" script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class RampUp : NetworkBehaviour {
    6.  
    7.     void OnTriggerEnter(Collider target){
    8.         if (target.tag == "Hand_NoTeam" || target.tag == "Hand_Green" || target.tag == "Hand_Orange" || target.tag == "Hand_Blue") {        
    9.             CmdRampUp();    
    10.         }
    11.     }
    12.  
    13.     [TargetRpc]
    14.     public void TargetRampUp(NetworkConnection target){
    15.         Debug.Log ("Cheeseburger!");
    16.     }
    17.  
    18.     [Command]
    19.     void CmdRampUp (){
    20.         TargetRampUp (connectionToClient);
    21.     }
    22. }
    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?
     
  2. MichalBUnity

    MichalBUnity

    Unity Technologies

    Joined:
    May 9, 2016
    Posts:
    18
  3. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62
    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:


    Code (CSharp):
    1. [ClientRpc]
    2.     void RpcTag_NoTeam(GameObject player){
    3.         player.GetComponent<SpriteRenderer> ().sprite  = noTeam;
    4.         player.GetComponent<LineRenderer> ().material = noTeam_arm;
    5.         player.tag = "Team_NoTeam";
    6.         player.gameObject.transform.GetChild (0).gameObject.tag = "Hand_NoTeam";
    7.         player.gameObject.transform.GetChild (0).gameObject.GetComponent<SpriteRenderer> ().sprite = noTeam_nucleus;
    8.     }
    9.  
    10.     [Command]
    11.     public void CmdTag_NoTeam(GameObject player) {
    12.         RpcTag_NoTeam (player); // Use RPC function to change attriutes
    13.     }
    I just can't figure out how to identify single clients.
     
  4. MichalBUnity

    MichalBUnity

    Unity Technologies

    Joined:
    May 9, 2016
    Posts:
    18
    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.
     

    Attached Files:

  5. Di_o

    Di_o

    Joined:
    May 10, 2016
    Posts:
    62

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

    Code (CSharp):
    1. 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 :)


    /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 ! :)
     
    Last edited: Jun 29, 2016
    MichalBUnity likes this.