C# Multiplayer Errors

hey guys, i’m new here and was wondering if u could help me out with this code:

using UnityEngine;
using System.Collections;
using System;

public class MPBase : MonoBehaviour
{

    public string connectToIP = "127.0.0.1";
    public int connectPort = 25000;
    public bool useNAT = false;
    public string ipaddress = "";
    public string port = "";

    string playerName = "<NAME ME>";

    void onGUI()
    {
        if (Network.peerType == NetworkPeerType.Disconnected)
        {
            if (GUILayout.Button("Connect"))
            {
                if (playerName != "<NAME ME>")
                {
                    Network.useNat = useNAT;
                    Network.Connect(connectToIP, connectPort);
                    PlayerPrefs.SetString("playerName", playerName);
                }
            }
            if (GUILayout.Button("Start Server"))
            {
                if (playerName != "<NAME ME>")
                {
                    Network.useNat = useNAT;
                    Network.InitializeServer(32, connectPort);

                    foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
                    {
                        go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
                    }
                    PlayerPrefs.SetString("PlayerName", playerName);
                }
            }
            playerName = GUILayout.TextField(playerName);
            connectToIP = GUILayout.TextField(connectToIP);
            connectPort = Convert.ToInt32(GUILayout.TextField(connectPort, ToString()));
        }
        else
        {
            if (Network.peerType == NetworkPeerType.Connecting) GUILayout.Label("Connect Status: Connecting");
            else if (Network.peerType == NetworkPeerType.Client)
            {
                GUILayout.Label("Connection Status: Client!");
                GUILayout.Label("Ping To Server: " + Network.GetAveragePing(Network.connections[0]));
            }
            else if (Network.peerType = NetworkPeerType.Server)
            {
                GUILayout.Label("Connection Status: Server!");
                GUILayout.Label("Connections: " + Network.connections.Length);
                if(Network.connections.Length >= 1)
                    GUILayout.Label("Ping To Server: " + Network.GetAveragePing(Network.connections[0]));
            }

            if (GUILayout.Button("Disconnect"))
                Network.Disconnect(200);

            ipaddress = Network.player.ipAddress;
            port = Network.player.port.ToString();
            GUILayout.Label("IP Address: " + ipaddress + " : " + port);
        }
    }

    void OnConnectedToServer()
    {
        foreach (GameObject go in FindObjectsOfType(typeof(GameObject)))
        {
            go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
        }
    }
}

125alt text125

Could you explain exactly what is going wrong here? It's difficult to just look at some code and know straight off what the problem is.

–

3 Answers

3

replace this code with yours

connectPort = Convert.ToInt32(GUILayout.TextField(connectPort.ToString()));

else if (Network.peerType == NetworkPeerType.Server)

i think above two lines where u got the error so replace those lines with these these lines
you will rectify ur errors.

these are simple errors

in first line instead of “.” u have taken ",’

in second u have missed “=” this sign

so u got the errors

hello term1702 it is a simple one

you are using “onGUI” change it into “OnGUI” it will work.

u have taken small “o” instead of capital “O”.

hello term1702

i think u did not given the ip address correct.

the default ip address is given as “127.0.0.1” now change this one to to your local ip address such as “196.168.0.1” the given one is just an example so u can use your local ip address that u want to connect to your system to another system.