- Home /
Networking bug.
I have 3 network scripts that I have attached one(networkmanager) that is on and observe by the players network view, networkmenu, on a object called network(this object has a network view and is observing the transform). Lastly a score object with the score script(with a net-view). Though everything works fine in the engine host or as client. Then on the build the player can control but he can't use the mouse look and there is a bug where the skybox is flashing through the terrain?!? This never happend before please help me.
NetworkManager:
using UnityEngine; using System.Collections;
public class NetworkManager : MonoBehaviour {
public bool Canmove;
public bool Activated;
public bool isAlive;
public Vector3 CurrentPosition;
public Quaternion CurrentRotation;
public Transform ControllerTransform;
public GameObject OutsideView;
public GameObject NetworkScoreManager;
public int Health = 100;
public string _name;
private bool pausemenu;
public CharacterController cont;
// Use this for initialization
void Start () {
Health = 100;
isAlive = true;
NetworkScoreManager = GameObject.FindGameObjectWithTag("ScoreManager");
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Escape)){
pausemenu = !pausemenu;
}
//HandleAnimations();
if(networkView.isMine && isAlive){
ControllerTransform.gameObject.SetActive(true);
OutsideView.SetActive(false);
}
if(!networkView.isMine && isAlive){
OutsideView.SetActive(true);
ControllerTransform.gameObject.SetActive(false);
}
if(NetworkMenu.ins.matchstarted){
Canmove = true;
}
if(!Canmove){
ControllerTransform.gameObject.GetComponent<CharacterMotor>().enabled = false;
}
else{
ControllerTransform.gameObject.GetComponent<CharacterMotor>().enabled = true;
}
if(!isAlive){
ControllerTransform.gameObject.SetActive(false);
OutsideView.SetActive(false);
}
}//End Update
//OnGUI
void OnGUI(){
if(networkView.isMine){
GUI.Box(new Rect(Screen.width - 125,5,120,24),"Health: " + Health);
if(Network.isServer){
GUI.Box(new Rect(Screen.width - 125,30,120,25),"Server");
}
if(Network.isClient){
GUI.Box(new Rect(Screen.width - 125,30,120,25),"Client");
}
if(pausemenu){
if(GUI.Button(new Rect(Screen.width / 2 - 100,Screen.height / 2 - 30,200,25),"Leave")){
NetworkMenu.ins.PlayerLeave(gameObject);
Network.Disconnect();
Application.LoadLevel(0);
}
if(GUI.Button(new Rect(Screen.width / 2 - 100,Screen.height / 2,200,25),"Quit Game")){
NetworkMenu.ins.PlayerLeave(gameObject);
Network.Disconnect();
Application.Quit();
}
}
}
}
public void HandleAnimations(){
if(networkView.isMine){
if(cont.velocity.magnitude > 0.1f){
networkView.RPC("SyncTheAnimations",RPCMode.Others,"Walking");
}
if(cont.velocity.magnitude < 0.1f){
networkView.RPC("SyncTheAnimations",RPCMode.Others,"Idle");
}
}
}
//End GUI
void FixedUpdate()
{
if (networkView.isMine)
{
CurrentPosition = ControllerTransform.position;
CurrentRotation = ControllerTransform.rotation;
}
else
{
OutsideView.transform.position = CurrentPosition;
OutsideView.transform.rotation = CurrentRotation;
}
}
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
if (stream.isWriting)
{
stream.Serialize(ref CurrentPosition);
stream.Serialize(ref CurrentRotation);
}
else
{
stream.Serialize(ref CurrentPosition);
stream.Serialize(ref CurrentRotation);
}
}
private void Die(){
}
private void HealthManager(){
if(Health < 1){
isAlive = false;
}
}
//RPCs
[RPC]
void Client_TakeDamage(int Damage){
Health-=Damage;
if(Damage >= Health){
HealthManager();
}
}
[RPC]
void SyncTheAnimations(string animation){
if(!networkView.isMine){
OutsideView.animation.Play(animation);
}
}
[RPC]
void KickPlayer(string kickedname){
if(kickedname == _name){
Debug.Log("Kickin you out!");
NetworkMenu.ins.PlayerLeave(gameObject);
}
}
}
NetworkMenu:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class NetworkMenu : MonoBehaviour {
public string CurrentMenu = "";
public string servername = "Server Name";
public int Port = 24444;
public List<MPPlayer> PlayerList = new List<MPPlayer>();
public GameObject Spawnpoint;
public GameObject Spawnedpl;
public bool matchstarted;
public bool Connected;
public string _Playername;
public static NetworkMenu ins;
public GameObject networkScoreManager;
public ScoreManager score;
void Awake(){
ins = this;
NextMenu("Main");
networkScoreManager = GameObject.FindGameObjectWithTag("ScoreManager");
score = networkScoreManager.GetComponent<ScoreManager>();
_Playername = PlayerPrefs.GetString("PlayerName");
}
void OnGUI(){
if(CurrentMenu == "Main"){
GUIMain();
}
if(CurrentMenu == "Start"){
GUIStart();
}
if(CurrentMenu == "Browser"){
GUIBrowser();
}
Lobby();
}
//GUI
private void GUIMain(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Browser")){
NextMenu("Browser");
}
if(GUI.Button(new Rect(10,40,200,25),"Host")){
NextMenu("Start");
}
if(GUI.Button(new Rect(10,70,200,25),"Quit")){
Application.Quit();
}
}
}
private void GUIStart(){
if(!Network.isClient && !Network.isServer){
_Playername = GUILayout.TextArea(_Playername,15);
if(GUILayout.Button("Save Name")){
PlayerPrefs.SetString("PlayerName", _Playername);
}
if(GUILayout.Button("StartServer")){
StartServer();
}
}
}
void Update(){
if(Network.isServer){
if(Network.connections.Length == 0 && !matchstarted){
//Server_SendSettings();
//Spawn();
}
}
}
public void Server_SendSettings(){
networkView.RPC("Player_LoadSettings",RPCMode.All,true);
networkScoreManager.networkView.RPC("FoundAllPlayers",RPCMode.All,true,Network.connections.Length + 1);
Debug.Log("Sending Match Settings");
}
public void Lobby(){
if(Network.isServer && !matchstarted){
if(GUILayout.Button("Start")){
Server_SendSettings();
}
}
}
private void GUIBrowser(){
if(!Network.isClient && !Network.isServer){
if(GUI.Button(new Rect(10,10,200,25),"Back")){
NextMenu("Main");
}
if(GUI.Button(new Rect(10,40,200,25),"Refresh")){
MasterServer.RequestHostList("354545Battle");
}
GUILayout.BeginArea(new Rect(Screen.width / 2, 0,Screen.width / 2,Screen.height),"Server List","box");
GUILayout.Space(20);
foreach(HostData hd in MasterServer.PollHostList()){
GUILayout.BeginHorizontal();
GUILayout.Label(hd.gameName);
if(GUILayout.Button("Connect")){
Network.Connect(hd);
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
}
void OnConnectedToServer(){
if(matchstarted){
Network.Disconnect();
Application.LoadLevel(0);
}
}
public void PlayerLeave(GameObject player){
Network.Destroy(player);
networkScoreManager.networkView.RPC("PlayerLeft",RPCMode.All,_Playername);
}
public void HostQuit(GameObject host){
networkView.RPC("KickAllPlayers",RPCMode.All);
}
void OnServerInitialized(){
}
public void StartServer(){
Network.InitializeServer(15,Port,false);
// Network.InitializeSecurity();
MasterServer.RegisterHost("354545Battle",servername,"Hosted Game");
}
public void Spawn(){
GameObject PlayerSpawned = Network.Instantiate(Spawnedpl,Spawnpoint.transform.position,Quaternion.identity,1) as GameObject;
PlayerSpawned.GetComponent<NetworkManager>()._name = _Playername;
}
//Next Menu Void
public void NextMenu(string nxt){
CurrentMenu = nxt;
}
void OnDisconnectedFromServer(){
Application.LoadLevel(0);
}
void OnPlayerDisconnected(NetworkPlayer id){
Network.DestroyPlayerObjects(id);
Network.RemoveRPCs(id);
}
//RPCs
[RPC]
void Player_Joined(){
}
[RPC]
void Player_LoadSettings(bool _matchstarted){
matchstarted = _matchstarted;
Debug.Log("Got Settings!");
Spawn();
}
}
public class MPPlayer{ public string PlayerName; public NetworkPlayer PlayerNetwork; }
ScoreManager:
using UnityEngine; using System.Collections;
public class ScoreManager : MonoBehaviour { public int AlivePlayers; public bool Matchstarted; // Use this for initialization void Start () {
}
// Update is called once per frame
void Update () {
}
[RPC]
void FoundAllPlayers(bool started, int players){
Debug.Log("I got the players!!! Number " + players);
AlivePlayers = players;
Matchstarted = true;
}
[RPC]
void APlayerDied(string _name){
AlivePlayers--;
}
[RPC]
void PlayerLeft(string _name){
Debug.Log("Player: " + _name + " Left the game.");
AlivePlayers--;
}
}
Your answer
Follow this Question
Related Questions
Server doesnt run scripts of objects out of camera 2 Answers
GUI Windows not being removed properly in a network 1 Answer
OnDisconnectedFromServer ignores inheritance 1 Answer
Player-Network-Bug 1 Answer
RPC sync issues 0 Answers