Bigwater
Bigwater

Reputation: 223

Unknown C# Error

Hi I don't know why C# is having a parsing error on my last Curly Bracket, sorry if it is very simple im new to C# here is the code.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
    public string IP = "192.168.0.8";
    public int Port = "25001";

    void OnGUI()
    {
        if(Network.peerType == NetworkPeerType.Disconnected)
        {
            if (GUI.Button(new Rect(100,100,100,25),"Join Existing Server"))
            {
                Network.Connect(Ip,Port);    
            }

            if (GUI.Button(new Rect(100,125,100,25),"Create New Server"))
            {
                Network.InitializeServer(10,Port);
                //First Number Above next to port in perenthisies is number of allowed clients / 1x Server (# of players allowed to join game.)
            }
            else 
            {
                if(Network.peerType == NetworkPeerType.Client)    
                {
                    GUI.Label(new Rect(100,100,100,25),"Client");
                    if(GUI.Button (new Rect(100,125,100,25),"Disconnect"))
                    {
                        Network.Disconnect(250);
                    }
                }
                if(Network.peerType == NetworkPeerType.Server)
                {
                    GUI.Label(new Rect(100,100,100,25),"Server");
                    GUI.Label(new Rect(100,125,100,25),"Connections: " + Network.connections.Length);

                    if(GUI.Button (new Rect(100,125,100,25),"Disconnect"))
                    {
                        Network.Disconnect(250);

                }
            }
        }
    }
}

The last "}" is coming up red any help would be appreciated thanks!

Just so people don;t get confused it is supposed to be a server and client for an on-line video game but it won't let me run the game with the error. I have tried to improve the formatting and remove blank space but none seemed to work, I have also made sure that there are the right amount of closing bracket at the end of the code so I have no Idea what is wrong.

Upvotes: 1

Views: 360

Answers (4)

rhughes
rhughes

Reputation: 9583

I appreciate the question is not to do with this, but just to help, the error regarding:

cannot implicitly convert type string to int

can be solved by changing your code to the following:

public class NewBehaviourScript : MonoBehaviour {

    public string IP = "192.168.0.8";
    public string Port = "25001";

or

public class NewBehaviourScript : MonoBehaviour {

    public string IP = "192.168.0.8";
    public int Port = 25001;

Upvotes: 1

Saleh Parsa
Saleh Parsa

Reputation: 1433

This is your code with correction. Be careful about the brackets.

using UnityEngine;

using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

public string IP = "192.168.0.8";
    public int Port = "25001";

    void OnGUI()
    {

        if(Network.peerType == NetworkPeerType.Disconnected)
        {

            if (GUI.Button(new Rect(100,100,100,25),"Join Existing Server"))
            {

            Network.Connect(Ip,Port);   

            }



            if (GUI.Button(new Rect(100,125,100,25),"Create New Server"))
            {

            Network.InitializeServer(10,Port);
                //First Number Above next to port in perenthisies is number of allowed clients / 1x Server (# of players allowed to join game.)

            }

            else 
            {

            if(Network.peerType == NetworkPeerType.Client)  
                {


                    GUI.Label(new Rect(100,100,100,25),"Client");
                    if(GUI.Button (new Rect(100,125,100,25),"Disconnect"))
                    {

                        Network.Disconnect(250);


                    }


                }
                if(Network.peerType == NetworkPeerType.Server)
                {

                    GUI.Label(new Rect(100,100,100,25),"Server");
                    GUI.Label(new Rect(100,125,100,25),"Connections: " + Network.connections.Length);

                    if(GUI.Button (new Rect(100,125,100,25),"Disconnect"))
                    {

                        Network.Disconnect(250);

                    }
                }
            }
        }
    }
}

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66489

After formatting your code, I can see you're missing one final closing brace.

Add a closing brace } at the very end of your code snippet above.

Upvotes: 3

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 121037

Add a closing bracket after:

if(GUI.Button (new Rect(100,125,100,25),"Disconnect"))
{

    Network.Disconnect(250);

It's missing.

Upvotes: 7

Related Questions