0

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  

}
Devender Gupta
  • 496
  • 5
  • 22
  • Hey actually i know that the 0bject is set to null.. i want to know why is it set to null... Because the active player is seen in the inspector. And... Donno why its still null. – Devender Gupta May 01 '18 at 10:22
  • Don't you think you should show the line of code that's causing that exception? – Programmer May 01 '18 at 10:38
  • line 83 : GameObject ball = Instantiate(Ball,activePlayer.shooter.position,Quaternion.identity) as GameObject; this – Devender Gupta May 01 '18 at 10:46
  • Just use Debug.Log statements to pinpoint which object is wrong, and backtrack from there `Debug.Log ("Ball exists: " + Ball!=null);` `Debug.Log ("activePlayer exists: " + activePlayer!=null);` `Debug.Log ("activePlayer.shooter exists: " + activePlayer.shooter!=null);` – Chris H May 01 '18 at 19:45

0 Answers0