Unexpected Symbol 'break'
Hi I'm new in programming and there is this Unexpected Symbol 'break' that doesn't want get out no matter how many posts I read and tweaks I do. Here it is:
void Update ()
{
switch (state)
{
case 0:
if (GUI.Button (new Rect (10, 10, 100, 30), "Connect"))
Connect ();
break;
case 1:
(GUI.Label (new Rect (10, 10, 100, 30), "Connected"))
break; <----------------<-------------------<-----------------------<---------------------
}
}
Here is the full coding using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Game$$anonymous$$anager : Photon.$$anonymous$$onoBehaviour {
public GameObject[] Spawns;
public int state = 0;
void Connect()
{
PhotonNetwork.ConnectToBestCloudServer ("V0.1");
state = 1;
}
void Start ()
{
}
void Update ()
{
switch (state)
{
case 0:
if (GUI.Button (new Rect (10, 10, 100, 30), "Connect"))
Connect ();
break;
case 1:
(GUI.Label (new Rect (10, 10, 100, 30), "Connected"))
break;
}
}
}
Answer by Pengocat · Feb 06, 2017 at 04:53 AM
You are missing a semicolon in case 1. Note that the extra parenthesis is unnecessary for this line GUI.Label (new Rect (10, 10, 100, 30), "Connected");
. You could make an enum instead of setting a "magic" integer number as state.
public GameObject[] Spawns;
private enum ConnectionState
{
waitingToConnect,
connecting,
connected
}
private ConnectionState connectionState = ConnectionState.waitingToConnect;
public bool isATestBool;
// OnGUI is called for rendering and handling GUI events
void OnGUI()
{
switch (connectionState)
{
case ConnectionState.waitingToConnect:
if (GUI.Button(new Rect(10, 10, 100, 30), "Connect"))
connectionState = ConnectionState.connecting;
break;
case ConnectionState.connecting:
GUI.Label(new Rect(10, 10, 100, 30), "Connecting");
TryToConnect();
break;
case ConnectionState.connected:
GUI.Label(new Rect(10, 10, 100, 30), "Connected");
//Invoke("TestReset", 3f);
break;
default:
Debug.LogErrorFormat("connectionState not properly set in class:{0}", this);
break;
}
}
private void TryToConnect()
{
// TODO Replace isTestBool with a photon callback that confirms the connection
if (isATestBool )
{
// Connection succeeded
connectionState = ConnectionState.connected;
}
else
{
// Still waiting for connection to complete
connectionState = ConnectionState.connecting;
}
}
//private void TestReset()
//{
// connectionState = ConnectionState.waitingToConnect;
//}
I left some test code in it that you could experiment with. Note the TODO comment. You should get a confirmation from photon that you got a connection before you display it. isATestBool
should be replaced by your own bool check.
Answer by AR_Rizvi · Feb 06, 2017 at 05:28 AM
What you are trying to do is not breaking the line which is in your second case you should use ; or semicolon to break a line if you are done with it and please use Curly brackets after all if statements it is good practice
Here i have edited it
void Update()
{
switch (state)
{
case 0:
if (GUI.Button (new Rect (10, 10, 100, 30), "Connect"))
{
Connect ();
}
break;
case 1:
GUI.Label (new Rect (10, 10, 100, 30), "Connected");
break;
}
}
Your answer
Follow this Question
Related Questions
FormatException: Input string was not in the correct format 0 Answers
What is wrong with my rotation code? (i am new to coding) 1 Answer
What is wrong with my script? 0 Answers
Particles appearing behind background image 0 Answers
how do i remember a just instantiated prefab as a variable gameobject? 1 Answer