how i can fix this
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MultiplayerManager : MonoBehaviour {
public static MultiplayerManager instance;
public string PlayerName;
private string MatchName = "";
private string MatchPassword = "";
private int MatchMaxUsers = 32;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
void Awake()
{
NetworkView = GetComponent<NetworkView> ();
networkView = GetComponent<NetworkView> ();
}
void Start()
{
instance = this;
}
public void StartServer(string servername, string serverpassword, int maxusers)
{
MatchName = servername;
MatchPassword = serverpassword;
MatchMaxUsers = maxusers;
Network.InitializeServer (MatchMaxUsers, 2550, false);
Network.InitializeSecurity ();
}
void OnServerInitialized()
{
Server_PlayerJoinRequest ("", Network.player);
}
void OnConnectedToServer()
{
GetComponent<NetworkView>().RPC ("Server_PlayerJoinRequest", RPCMode.Server, "", Network.player);
}
void OnPlayerDisconnected(NetworkPlayer id)
{
GetComponent<NetworkView>().RPC ("Client_RemovePlayer", RPCMode.All, id);
}
[RPC]
void Server_PlayerJoinRequest(string playername, NetworkPlayer view)
{
GetComponent<NetworkView>().RPC ("Client_AddPlayerToList", RPCMode.All, playername, view);
}
[RPC]
void Client_AddPlayerToList(string playername, NetworkPlayer view)
{
MPPlayer tempplayer = new MPPlayer ();
tempplayer.PlayerName = playername;
tempplayer.PlayerNetwork = view;
PlayerList.Add (tempplayer);
}
[RPC]
void Client_RemovePlayer(NetworkPlayer view)
{
MPPlayer temppl = null;
foreach (MPPlayer pl in PlayerList)
{
if(pl.PlayerNetwork == view)
{
temppl = pl;
}
}
if (temppl != null) {
PlayerList.Remove(temppl);
}
}
}
public class MPPlayer
{
public string PlayerName = "";
public NetworkPlayer PlayerNetwork;
}
MultiplayerManager.cs(19,17): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer
MultiplayerManager.cs(20,17): error CS0200: Property or indexer `UnityEngine.Component.networkView' cannot be assigned to (it is read only)
Answer by Dave-Carlile · Aug 30, 2015 at 03:53 PM
For the first error:
NetworkView = GetComponent<NetworkView> ();
Most programming languages are case sensitive, meaning NetworkView
is not the same as networkView
. In this case you're assigning the GetComponent
result to NetworkView
, which identifies a class, not a variable. You can't do this, so you get the error.
For the second error:
networkView = GetComponent<NetworkView> ();
A game object has a built-in property called networkView which automatically references any NetworkView
component that's on the object. The property is read only, but you're trying to assign to it, which you can't.
However, that property is deprecated, which means it still exists for now but will going away in some future version. So you should declare your own networkView
at the top of your class, then your assignment will work.
private string MatchName = "";
private string MatchPassword = "";
private int MatchMaxUsers = 32;
private NetworkView networkView; // <-- declare this at the top and your `networkView =` code will work.
Make sure to remove the line that starts NetworkView =
- the one with the upper case N.