Ok I am using C# and trying to write a switch to place scripts on everything containing certain names. I know I am doing this wrong but I am horrible at things like this so what would be the correct way?
I think you can kind of see what I am going fo in this code. Just dont know how to get it done correctly. any help?
//lets look for objects with the name "Valve" in it.
if (transform.name.ToLower().Contains("valve"))
{
//Apply a collision to the object that we just looked for (valve)
transform.gameObject.AddComponent(typeof(MeshCollider));
//Add a script to the object that we just looked for (valve)
string fullName = transform.name.ToLower()
switch(fullName)
{
case fullName.Contains("valveup"):
transform.gameObject.AddComponent("ValveUp");
break;
case fullName.Contains("valvedown"):
transform.gameObject.AddComponent("ValveDown");
break;
case fullName.Contains("valveleft"):
transform.gameObject.AddComponent("ValveLeft");
break;
case fullName.Contains("valveright"):
transform.gameObject.AddComponent("ValveRight");
break;
case fullName.Contains("valveforward"):
transform.gameObject.AddComponent("ValveForward");
break;
case fullName.Contains("valvebackward"):
transform.gameObject.AddComponent("ValveBackward");
break;
default:
transform.gameObject.AddComponent("Backup_ValveMove");
break;
}
}
//lets look for objects with the name "Valve" in it.
if (transform.name.ToLower().Contains("valve"))
{
//Apply a collision to the object that we just looked for (valve)
transform.gameObject.AddComponent(typeof(MeshCollider));
//Add a script to the object that we just looked for (valve)
string fullName = transform.name;
string[] importString = new string[] {"ValveUp", "ValveDown", "ValveLeft", "ValveRight", "ValveForward", "ValveBackward"};
foreach (string s in importString)
{
switch (fullName.Contains(s))
{
case true:
transform.gameObject.AddComponent(s);
break;
default:
//transform.gameObject.AddComponent("Backup_ValveMove");
break;
}
}
}
Same error as what? You never mentioned getting an error in the first post. You said that had done it wrong, and wanted to know how. Also, you’ve only posted half your code, so telling me the line number you get an error on is not telling me much.
What you’ve ended up with there shouldn’t be using switch…case at all. It would be more concise and more readable with an if… else.
More importantly, I don’t think it does what you think it does, or wanted to do any more. It’s going to put a copy of the Backup_ValveMove component on the object every time it goes through the loop and doesn’t find what it’s looking for. In other words, you’re going to get five copies of that component, in addition to any other it may add.
I think in this case you´ll have to use if (if (fullName.Contains(s)) { … } ) or think more carefully what you want to do. When you write:
switch (var)
{
case value :
…
}
the program will try to compare var with value. You can´t compare var with something inside itself. I think even this hack, switch (true) { case true: … }, won´t work for you in all cases, or you´ll have a really ugly code. It´s not worth it.