- Home /
Null reference Error in UNET
Using this script i can shoot the projectiles from Server to Client. But when i try to shoot the projectiles in the client it shows null reference error. Can someone help me?
This is the error :
NullReferenceException: Object reference not set to an instance of an object NetworkPlayerManager.CmdFire () (at Assets/Networked Scripts/NetworkPlayerManager.cs:83) NetworkPlayerManager.InvokeCmdCmdFire
The script is given below:
public class NetworkPlayerManager : NetworkBehaviour {
#region Variables
public GameObject Ball;
public float Power = 10f;
public PlayerController activePlayer;
public PlayerController player1,player2,player3;
public Renderer player1color,player2color,player3color;
public Transform PlayerFormation;
public Button b1,b2,b3;
public Canvas myCanvas;
public Camera myCamera;
private int playerNumber;
private Transform obj;
private string targetTag="Target";
#endregion
#region Built-in Functions
void Start ()
{
if (!isLocalPlayer)
{
AssignTags();
transform.name="Opponent Player- Not Local Player";
return;
}
}
void Update ()
{
if (!isLocalPlayer)
return;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag==targetTag&&!hit.transform.GetComponent<PlayerController>().is_hit)
{
obj=hit.transform;
CmdFire();
PlayerSelector();
}
}
}
}
#endregion
#region Override
public override void OnStartLocalPlayer()
{
ActivateEssentials();
ChangeColor();
AssignButtons();
TurnDecider();
if (isServer)
SetName("Host Player - Local Player");
else
SetName("Client Player - Local Player");
}
#endregion
#region Commands
[Command]
public void CmdFire()
{
GameObject ball = Instantiate(Ball,activePlayer.shooter.position,Quaternion.identity) as GameObject;
ball.GetComponent<Rigidbody>().velocity = (obj.position - activePlayer.shooter.position).normalized * Power;
NetworkServer.Spawn(ball);
}
#endregion
#region Client Rpc
[ClientRpc]
public void RpcFire()
{
if (!isServer)
{
print("Called RPC Fire()");
}
}
#endregion
#region Other Functions
public void SetName(string name)
{
transform.name=name;
}
public void AssignTags()
{
transform.tag=targetTag;
player1.tag=targetTag;
player2.tag=targetTag;
player3.tag=targetTag;
}
public void AssignButtons()
{
b1.onClick.AddListener(player1.Jump);
b2.onClick.AddListener(player2.Jump);
b3.onClick.AddListener(player3.Jump);
}
public void ActivateEssentials()
{
playerNumber = 1;
myCamera.gameObject.SetActive(true);
myCanvas.gameObject.SetActive(true);
}
public void ChangeColor()
{
player1color.material.color = Color.green;
player2color.material.color = Color.green;
player3color.material.color = Color.green;
}
public void PlayerSelector()
{
if (playerNumber == 3)
playerNumber = 1;
else
playerNumber++;
TurnDecider();
}
public void TurnDecider()
{
switch (playerNumber)
{
case 1:
player1.isReady=false;
player2.isReady = true;
player3.isReady = false;
activePlayer=player2;
break;
case 2:
player1.isReady=false;
player2.isReady = false;
player3.isReady = true;
activePlayer=player3;
break;
case 3:
player1.isReady=true;
player2.isReady = false;
player3.isReady = false;
activePlayer=player1;
break;
}
}
#endregion
}
Answer by alankemp · May 01, 2018 at 10:21 AM
If you understand the error message, it should be easy to track down where and what is going wrong.
"NullReferenceException: Object reference not set to an instance of an object" This means you are using a variable that is currently set to null. For example, maybe you forgot to "new" something, or forgot to assign a variable via the properties window.
"NetworkPlayerManager.CmdFire () (at Assets/Networked Scripts/NetworkPlayerManager.cs:83) " This is saying the problem happened on line 83 of NetworkPlayerManager.cs, (and its telling you the function you are in is called NetworkPlayerManager.CmdFire ()).
It is a bit hard to tell exactly which is line 83 from the way you have pasted your code, but here is the relevant function:
public void CmdFire() { GameObject ball = Instantiate(Ball,activePlayer.shooter.position,Quaternion.identity) as GameObject; ball.GetComponent<Rigidbody>().velocity = (obj.position - activePlayer.shooter.position).normalized * Power; NetworkServer.Spawn(ball); }
Ball is a variable that should be set via the properties on this game object in Unity. Did you set it?
Or maybe activePlayer is null?
You can either set a break point in this function and use the debugger to figure out which one is wrong, or write a series of Debug.Log statements to print out if the variables are null or not.
Hey i did actually check it! The variable activePlayer is null.
This is line 83:
GameObject ball = Instantiate(Ball,activePlayer.shooter.position,Quaternion.identity) as GameObject;
Your answer
Follow this Question
Related Questions
How to connect to our own dedicated server using photon networking in unity?(Self-hosted) 0 Answers
Multiple NetworkBehaviours with the same SyncVar hook not working 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can I see how many bytes are being sent by a particular SyncVar variable? 1 Answer