Ive been developing in C# in unity for a couple years. I get my games to work but struggling over trying to write consise re-usable code. I’ve been reading the “C# programming yellow book” and they say “Block copy is evil”
but how else would I for example write this code using only one if block. in this case I only have sidewalkA and sidewalkB so its not that bad but what if I had C,D,E etc
if (hit2.transform.tag == "SidewalkA")
{
for (int i = 0; i < SideWalk_listA.Count; i++)
{
if (SideWalk_listA[i].transform.position == hit2.transform.position)
{
for (int a = 1; a <= i; a++)
{
polesA[a].transform.position = SideWalk_listA[a].transform.position;
polesA[a].transform.rotation = SideWalk_listA[a].transform.rotation;
float sidewalk_length = Mathf.Max(SideWalk_listA[a].GetComponent<Renderer> ().bounds.size.x, SideWalk_listA[a].GetComponent<Renderer>().bounds.size.z);
if (sidewalk_length < 9f)
polesA[a].transform.Rotate(0, -45, 0);
}
break;
}
}
}
else if (hit2.transform.tag == "SidewalkB" )
{
for (int i = 0; i < SideWalk_listB.Count; i++)
{
if (SideWalk_listB[i].transform.position == hit2.transform.position)
{
for (int a = 1; a <= i; a++)
{
polesB[a].transform.position = SideWalk_listB[a].transform.position;
polesB[a].transform.rotation = SideWalk_listB[a].transform.rotation;
float sidewalk_length = Mathf.Max(SideWalk_listB[a].GetComponent<Renderer>().bounds.size.x, SideWalk_listB[a].GetComponent<Renderer>().bounds.size.z);
if (sidewalk_length < 9f)
polesB[a].transform.Rotate(0, -45, 0);
}
break;
}
}
}