0
votes
 public class GameControl : NetworkBehaviour {
 
     public GameObject localPlayer;
 
     [TargetRpc]
     public void TargetGetLocalPlayer(NetworkConnection conn)
     {
         localPlayer = GameObject.Find("Local");
       
     }
      public override void OnStartServer()
     {
        base.OnStartServer();
        TargetGetLocalPlayer(connectionToClient);
        
      }
   
 }

i have a script attached to a server object which should be able to grab the local player GameObject (which i denoted by changing it's name to 'Local' once it's spawned in another script) from the client but when i try to call TargetGetLocalPlayer , i get the following error :

 Exception in OnStartServer:Object reference not set to an instance of an object   at 
 UnityEngine.Networking.NetworkBehaviour.SendTargetRPCInternal 
 (UnityEngine.Networking.NetworkConnection conn, UnityEngine.Networking.NetworkWriter writer, 
 System.Int32 channelId, System.String rpcName) [0x0002e]

i am totally new to networking in unity and i feel like i should have gone with photon instead of unet , it seems like no one is interested in unet anymore and the docs suck at explaining anything and i will be very grateful if anyone could answer me , thanks in advance

2
As to it seems like no one is interested in unet anymore: UNet is deprecated .. Photon currently is the best optionderHugo

2 Answers

0
votes

I think a better solution would be to attach a script to each player. and make it so that when a "new" player joins it runs a method in your GameControl to add the player to a player list. like this:

    //this is on your gameControl.
    public List<Player> players;

    public void AddPlayer(Player player)
    {
       players.Add(player);
    }

this is working if you have your GameControl as a singleton. if you do not know how to do that check the last piece of code. //this is on your Player script called player(if you have another name change all //Player scripts in here to that name

      public void Start()
   {
       GameControl.AddPlayer(this);
   }

Or

Instead of making the players list a List you can make it a dictionary and make a key for each player who joins to make it more accesible.

How to make a script a singleton and why.

why:

if you have a manager class/script you always want there to only be ONE instance of it so you can call its methods easily and without problems.

How:

Basically you make it so that Only THIS script can change values and variables in the manager, and other scripts can get/call methods and functions. this makes it easily accesible and you will have less problems.

private static GameControl _GameControl;

  private Player player;
public static GameControl gameControl
{
    get
    {
        if(_GameControl == null)
        {
            Debug.LogError("Game Control is null");
            
        }

        return _GameControl;
    }
}


    void Awake()
   {
       _GameControl = this;
       player = GameObject.Find("Player").GetComponent<Player>();
    }
0
votes

Well i see what you mean, well create a method that can be run by any script like a singleton. then you pass in the gameobject that you want to add like this:

public class GameManager
{

    public GameObject _player;

    //this is a singleton.
    private static GameManager _gm;
    public static GameManager gameManager
    {
        get
        {
            if(_gm == null)
            {
                Debug.LogError("Game manager is null");
            }

            return _gm;
        }
    }


    void awake()
    {
        _gm = this;
    }


    void GetPlayer(GameObject player)
    {
        _player = player;
    }

    void AddPlayer(GameObject player)
    {
        //add it to whatever you want to.
    }
}

call the method this way:

public class Player : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameManager.gameManager.GetPlayer(this.gameObject);
    }

   
}