- Home /
[CLOSED] 2 errors but i don't know exactly (including picture)
Hello everybody! I have a problem, this script isn't made by myself. I've made some changes and I added things. But now I have these problems, I don't even know what they mean... Can somebody help me with this? If you can help, thank you :D
using System.Collections;
using UnityEngine;
//List of all the posible gamestates
public enum GameState
{
NotStarted,
Playing,
Completed,
Failed
}
//Make sure there is always an AudioSource component on the GameObject where this script is added.
[RequireComponent(typeof(AudioSource))]
public class GameManager : MonoBehaviour
{
//Text element to display certain messages on
public GUIText FeedbackText;
//Text to be displayed when entering one of the gamestates
public string GameNotStartedText;
public string GameCompletedText;
public string GameFailedText;
//Sounds to be played when entering one of the gamestates
public AudioClip StartSound;
public AudioClip FailedSound;
private GameState currentState = GameState.NotStarted;
//All the blocks found in this level, to keep track of how many are left
private Block[] allBlocks;
private Ball[] allBalls;
// Use this for initialization
void Start()
{
//Find all the blocks in this scene
allBlocks = FindObjectsOfType(typeof(Block)) as Block[];
//Find all the balls in this scene
allBalls = FindObjectsOfType(typeof(Ball)) as Ball[];
//Prepare the start of the level
SwitchTo(GameState.NotStarted);
}
// Update is called once per frame
void Update()
{
switch (currentState)
{
case GameState.NotStarted:
//Check if the player taps/clicks.
if (Input.GetMouseButtonDown(0)) //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
{
for (int i = 0; i < allBalls.Length; i++)
allBalls[i].Launch();
SwitchTo(GameState.Playing);
}
break;
case GameState.Playing:
{
bool allBlocksDestroyed = true;
//Check if all blocks have been destroyed
for (int i = 0; i < allBlocks.Length; i++)
{
if (!allBlocks[i].BlockIsDestroyed)
{
allBlocksDestroyed = false;
break;
}
}
//Are there no balls left?
if (FindObjectOfType(typeof(Ball)) == null)
SwitchTo(GameState.Failed);
if (allBlocksDestroyed)
SwitchTo(GameState.Completed);
switch(currentState)
{
case GameState.Failed:
Application.LoadLevel("Menu");
case GameState.Completed:
Application.LoadLevel("Level 2");
//Check if the player taps/clicks.
if (Input.GetMouseButtonDown(0)) //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
Restart();
break;
}
}
}
}
//Do the appropriate actions when changing the gamestate
public void SwitchTo(GameState newState)
{
currentState = newState;
switch (currentState)
{
case GameState.Playing:
audio.PlayOneShot(StartSound);
DisplayText("");
break;
case GameState.Completed:
audio.PlayOneShot(StartSound);
DisplayText(GameCompletedText);
StartCoroutine(RestartAfter(StartSound.length));
break;
case GameState.Failed:
audio.PlayOneShot(FailedSound);
DisplayText(GameFailedText);
StartCoroutine(RestartAfter(FailedSound.length));
break;
default:
case GameState.NotStarted:
DisplayText(GameNotStartedText);
break;
}
}
//Helper to display some text
private void DisplayText(string text)
{
FeedbackText.text = text;
}
//Coroutine which waits and then restarts the level
//Note: You need to call this method with StartRoutine(RestartAfter(seconds)) else it won't restart
private IEnumerator RestartAfter(float seconds)
{
yield return new WaitForSeconds(seconds);
Restart();
}
//Helper to restart the level
private void Restart()
{
Application.LoadLevel(0);
}
}
I'm not sure about differences between uJS and C# but in at least one of them, you $$anonymous$$UST include the default case.
@meat5000, you don't need a default, but you do need break statements if you do work in a case and attempt to fall through.
$$anonymous$$SDN about switch statements
$$anonymous$$ore to the point:
Each case label specifies a constant value. The switch statement transfers control to the switch section whose case label matches the value of the switch expression (caseSwitch in the example). If no case label contains a matching value, control is transferred to the default section, if there is one. If there is no default section, no action is taken and control is transferred outside the switch statement.
thanks tanoshimi, meat5000 and Landern for your help and suggestion. I learn from these comments :D
Answer by darthtelle · Aug 26, 2014 at 12:52 PM
From what I can tell you're missing a break statement in your Update call for the cases GameState.Playing and GameState.Failed.
Answer by Landern · Aug 26, 2014 at 01:06 PM
You have a couple issues, i updated it in your other post i responded to and @tanoshimi is correct.
You're missing break statements, you have default above a case statement, default is default, you can fall through other cases to a default, but you can't fall through the default to it a case like you did here:
default:
case GameState.NotStarted:
DisplayText(GameNotStartedText);
break;
Lets say i mock together your Classes(block and ball) to get this going on my end, stubbing in all the methods/fields from ball and block and attack this scripts errors.
Error #1(Ends up on line: 62 on mine): ~GameManager.cs(3,3): Error CS0163: Control cannot fall through from one case label ('case 1:') to another (CS0163) (Assembly-CSharp)
Lets look at what it's referring to:
case GameState.Playing:
{
bool allBlocksDestroyed = true;
//Check if all blocks have been destroyed
for (int i = 0; i < allBlocks.Length; i++) {
if (!allBlocks [i].BlockIsDestroyed) {
allBlocksDestroyed = false;
break;
}
}
//Are there no balls left?
if (FindObjectOfType (typeof(Ball)) == null)
SwitchTo (GameState.Failed);
if (allBlocksDestroyed)
SwitchTo (GameState.Completed);
switch (currentState) {
case GameState.Failed:
Application.LoadLevel ("Menu");
case GameState.Completed:
Application.LoadLevel ("Level 2");
//Check if the player taps/clicks.
if (Input.GetMouseButtonDown (0)) //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
Restart ();
break;
}
}
Notice that case GameState.Playing has breaks in it, but doesn't cover the full case itself. If there was a return at the end, that would satisfy the need for the break, but there isn't... so we bend and add one in there:
case GameState.Playing:
{
bool allBlocksDestroyed = true;
//Check if all blocks have been destroyed
for (int i = 0; i < allBlocks.Length; i++) {
if (!allBlocks [i].BlockIsDestroyed) {
allBlocksDestroyed = false;
break;
}
}
//Are there no balls left?
if (FindObjectOfType (typeof(Ball)) == null)
SwitchTo (GameState.Failed);
if (allBlocksDestroyed)
SwitchTo (GameState.Completed);
switch (currentState) {
case GameState.Failed:
Application.LoadLevel ("Menu");
case GameState.Completed:
Application.LoadLevel ("Level 2");
//Check if the player taps/clicks.
if (Input.GetMouseButtonDown (0)) //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
Restart ();
break;
}
break;
}
BOOM error #1 gone, how error #2, it's actually another case statement in the last case, you didn't break whatsoever which means you want it to do work(Load menu level/scene) but also want it to fall through and do work(load Level 2 level/scene)... NO DICE!
The offending code:
switch (currentState)
{
case GameState.Failed:
Application.LoadLevel ("Menu");
case GameState.Completed:
Application.LoadLevel ("Level 2");
if (Input.GetMouseButtonDown (0)) //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
Restart ();
break;
}
You notice case GameState.Failed doesn't have a break statement, you need one!
switch (currentState)
{
case GameState.Failed:
Application.LoadLevel ("Menu");
break;
case GameState.Completed:
Application.LoadLevel ("Level 2");
if (Input.GetMouseButtonDown (0)) //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
Restart ();
break;
}
After those two fixes, no more errors, builds fine.
Thank you so much!!!
But the last one isn't totally solved..
Line 50 is the line which starts with switch (currentState) {
// Update is called once per frame
void Update()
{
switch (currentState) {
case GameState.NotStarted:
//Check if the player taps/clicks.
if (Input.Get$$anonymous$$ouseButtonDown (0)) { //Note: on mobile this will translate to the first touch/finger so perfectly multiplatform!
for (int i = 0; i < allBalls.Length; i++)
allBalls [i].Launch ();
SwitchTo (GameState.Playing);
}
break;
case GameState.Playing:
{
bool allBlocksDestroyed = true;
//Check if all blocks have been destroyed
for (int i = 0; i < allBlocks.Length; i++) {
if (!allBlocks [i].BlockIsDestroyed) {
allBlocksDestroyed = false;
break;
}
}
//Are there no balls left?
if (FindObjectOfType (typeof(Ball)) == null)
SwitchTo (GameState.Failed);
if (allBlocksDestroyed)
SwitchTo (GameState.Completed);
Your answer
Follow this Question
Related Questions
Building Game w/ Scriptable Objects 1 Answer
Unity fails to recognize Maya models/Unity scenes 1 Answer
A node in a childnode? 1 Answer
Unity bug!! (HELP)!! 5 Answers
MonoDevelop Unhandled Exception. MonoDevelop will now close - Mac OSX 1 Answer