Hi,
Strange question, but I’m new to C# and I’m trying to differentiate between the two blocks of code.
So, this:
public List<Color> shapeColor = new List<Color>();
if(shapeColor.Count > 0)
{
int newColor = Random.Range(0, shapeColor.Count);
renderer.material.color = shapeColor[newColor];
}
And this:
public Color[] shapeColor;
if(shapeColor.Length > 0)
{
int newColor = Random.Range(0, shapeColor.Length);
renderer.material.color = shapeColor[newColor];
}
Both blocks seem to achieve the same thing - selecting a random colour from an array of colours selected in the inspector. My understanding of C# was that you should use List<> if the array of items you’re using is not a set length as it will produce errors otherwise. Here, the colours are picked in the Inspector window, so it can’t be defined in the code.
I had initially defined the shapeColor array as below:
public Color shapeColor[];
But ran into errors with the rest of the code because of the above. My question is, how is using Color any different and is could this cause any difficulties?
Ah! Thanks! Is there any different between: public Color shapeColor[]; and public Color[] shapeColor; My previous experience would have lead me to think that the top way was correct in this example, but it seems to be the other way around?
– GetUpKidAKNo the bottom way is right, though I've been known to find myself typing the top way and having to fix it ;) The point is you are defining a variable called shapeColor so it comes last. Then the type of that variable is an array of Color so you write that as the type. Hence the bottom one is right.
– whydoidoit