0
 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

AmirWG
  • 251
  • 1
  • 2
  • 10
  • As to `it seems like no one is interested in unet anymore`: [UNet is deprecated](https://support.unity3d.com/hc/en-us/articles/360001252086-UNet-Deprecation-FAQ) .. Photon currently is the best option – derHugo Sep 11 '20 at 06:46

2 Answers2

0

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>();
    }
Jadon Wolfgang
  • 180
  • 1
  • 12
  • is there a way to tell which of the players in the list is the player controlling the client ? that was the main problem. – AmirWG Sep 11 '20 at 00:55
  • well if there is an input you could get that as the controlling player. but if you have more players you will get more input ofc. – Jadon Wolfgang Sep 11 '20 at 01:36
  • but could you clarify what you mean with your question – Jadon Wolfgang Sep 11 '20 at 01:37
  • i need to know which of the Players in the Player's List is the one getting controlled by the client = the current local player on the client (and there are more than one player on the server ofc but there is ONLY one player controlling each CLIENT) – AmirWG Sep 11 '20 at 13:25
  • could you clarify what your *client* is. i do not understand because a player should be an instance of a player prefab . – Jadon Wolfgang Sep 11 '20 at 13:30
  • what i mean by client is the instance of the game in which the local player exists , i need only to get the player GameObject of the client of this player and i might be wrong about the client thing but as i said i am newbie at networking – AmirWG Sep 11 '20 at 13:39
  • i need to get the local player gameobject to to be able to spawn gameobjects with client authority – AmirWG Sep 11 '20 at 13:42
  • is there an id for each local player which can tell them apart? – AmirWG Sep 11 '20 at 13:52
0

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);
    }

   
}

Jadon Wolfgang
  • 180
  • 1
  • 12
  • @JadonWolfgand i tried what you said ,but i wanted to know if this singleton is the same for all clients(including the host) – AmirWG Sep 12 '20 at 16:39
  • 1
    it is of type static so yes. it will be the same for each client – Jadon Wolfgang Sep 12 '20 at 19:09
  • well it's the same instance for all game objects on the client but not the same for other clients , are you sure it should be the same ? – AmirWG Sep 13 '20 at 01:13
  • why do you have multiple game managers???? that would just make trouble but if you do it like that you will need to have 1 static class to share over all classes – Jadon Wolfgang Sep 14 '20 at 15:49