The following script is fine when var2 is given the valid switch expression value of 1 or 2. But when var2 is given 0, 3, etc, instead of switching to “default”, my Unity 5.5.0f3 freezes. Is there anything wrong with the code?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour {
public double var0,var1;
public int var2;
private double var3;
void Start () {
label:
Debug.Log ("blahblahblah");
switch (var2) {
case 1:
var3 = var0 + var1;
Debug.Log (var3);
break;
case 2:
var3 = var0 - var1;
Debug.Log (var3);
break;
default:
goto label;
}
}
void Update () {
}
}
I’d imagine it IS switching to default…and then you’re telling it to goto label, which is before the switch, so it goes through the switch again…and so on, into infinity. Which is why Unity freezes.
Thanks for your quick answer. I am following an online C# tutorial which isn’t meant for Unity and involves lot of console.writeline/readline. Obviously the switch example in that tutorial has console.readline to hold the script until user input which I replace them with public variable in Unity and debug.log to return the result. I read that you can setup a GUI in Unity to mimic a command console, but as an absolute beginner I have no luck in looking for such tutorial despite the veterans claim that “it’s very easy.” Thank you again.
You can set up a command console…but it’s definitely not an easy task for a beginning programmer. For a veteran, it’s very easy. I’d start with just learning how to get input from Unity’s UI system.
Agreed, Labels (outside of switch statements) and goto should never be needed in unity programming. its very uncommon today, especially in C# and it can just get really confusing to other programmers reading the code.
I used to use goto, labels, and ternary operators a ton in other languages, but only because of the limitations of the language itself (lack of control structures like for loops and if statements).
Today in C# however I avoid them like the plague. Except ternary operators, still love writing those.
I repeat what BoredMormon said, nearly everything you need to learn C# in Unity can be gained in Unity’s Learn and Live Training sections. its one of the best places on the web for teaching newcomers and beginners, and its free.
Unity’s Scripting API is worthy of a bookmark, even if some of the documentation can be ambiguous at times,
StackOverflow is also a fantastic resource when you’re googling for a specific C# issue (and sometimes a Unity issue).
Goto is generally Very Bad. However, since you cannot “fall through” in C# switch statements, there is a valid use of goto within a switch statement to go to common shared code that two separate cases need to finish up with.
switch( name)
{
case "Kurt":
GiveOutCoffee();
goto case "Tom";
case "Bob":
GiveOutTea();
goto case "Tom";
case "Tom":
// we don't want to duplicate this code:
GiveOutPastries();
GiveOutPopsicles();
break;
default :
GiveOutNothing();
break;
}
}
In the above sample, the shared code is something you don’t want to copy/pasta everywhere, or necessarily make yet another method to wrap it up, particularly if it refers to local variables.
And if you need to goto in a switch statement, always goto forward, never backwards, just to keep yourself and others who might look at your code sane.
As a friend in high school said, “A million BASIC-ites might die, but a true PASCAL-ite will never GOTO free.”
I’ve never been a big fan of fall through. If the code is complex, a separate method is justified. If the code is not complex, there is no real harm done by repeating a couple of lines. I find reading fall through code more difficult then reading the duplicate code. It seems to be more of a nod to old school programmers rather then a useful feature.
It also useful to get out of a bunch of nested loops. But I don’t like it there either.
I will always implement anything rather than repeating, but I got fanatical about DRY after my thousandth bug or so from copy-paste code…but I definitely wouldn’t use the kind of fall through indicated. Kurt and Bob ALSO being Tom is enough to make your head hurt even when you’re used to inheritance.
I would much rather use hook functions than fall through. if you look at the example Kurt-Dekker posted you’ll notice that the algorithm can be abstracted to be the same for all four cases.
Serve Drink
Serve Pasteries
Serve Popsicles
so you can write a simple script with hooks so that deriving classes can latch on to while extending
virtual public void GiveOutDrink(){}
virtual public void GiveOutPasteries(){}
virtual public void GiveOutPopsicles(){}
All three of functions are hooks. They are virtual/abstract and empty functions that are merely placeholders for the deriving classes to insert their behavior in a predetermined order. so in a default case calling all three functions without overriding them is essentially the same as the “GiveoutNothing” case shown in the switch statement.
Kurt would overload GiveOutDrink to serve Coffee.
Bob would overload GiveOutDrink to serve Tea
and all three (Tom, Kurt, and Bob) would overload GiveOutPasteries() and GiveOutPopsicles()
the primary benefit to this is that when a new person is added you can overload GiveOutDrink to serve a soda, and without touching the base code. its really useful when you come across a class that could have a significant number of cases all of which that do basically the same abstract thing.
normally I’ll avoid using switch statements, with the exception that I’m just too lazy (or don’t foresee a need) to abstract it out to more classes.