Hi everyone,
New to Unity and C#, I have been breaking my head over the following for the last 3 hours.
I am trying to assign a random colour to a renderer by using a list (as I want to delete colours from it in a later stage, as well as make sure all the colours are being used).
I have the following code but it gives me an error on the last line. Any ideas?
private SpriteRenderer sr;
public List<Color[]> coloursList = new List<Color[]>();
void Start()
{
Color[] array1 = new [] { Color.red, Color.blue, Color.green };
coloursList.Add(array1);
sr = GetComponent<SpriteRenderer>();
int rand = Random.Range(0, coloursList.Count);
sr.material.color = coloursList[rand];
}
Line 7 above doesn’t do what you think it does, it just adds a single element which itself is a fixed array of colours.
I think you meant something along these lines:
private SpriteRenderer sr;
public List<Color> coloursList;
void Start()
{
coloursList = new List<Color>() { Color.red, Color.blue, Color.green };
sr = GetComponent<SpriteRenderer>();
int rand = Random.Range(0, coloursList.Count);
sr.material.color = coloursList[rand];
}
… or …
private SpriteRenderer sr;
public List<Color> coloursList = new List<Color>();
void Start()
{
coloursList.Add(Color.red);
coloursList.Add(Color.blue);
coloursList.Add(Color.green);
sr = GetComponent<SpriteRenderer>();
int rand = Random.Range(0, coloursList.Count);
sr.material.color = coloursList[rand];
}
2 Likes
Thanks a lot!
How would I go about deleting the chosen colour from this list?
When I add
coloursList.Remove([rand]);
it gives me an error…
Because it’s against the syntax. What you do think you’re achieving with symbols?
You need to learn about the basics first, learn C# syntax and what the symbols mean.
I have put these in as I wanted to get the index of rand to be deleted from the list. Not having these would give me the error that rand is an int and not a colour. Going through documentation for hours have not helped me so far, as has your comment. I understand these beginner question must tire you, but you do not need to respond.
I am sorry I can’t seem to be able to figure it out from this https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?redirectedfrom=MSDN&view=net-7.0#System_Collections_Generic_List_1_Remove__0_
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-7.0
Look at the other methods available on List.
Hint: Anything else starting with “Remove”
It’s to push you into right direction. If I write the solution for you, you’re likely to never learn or remember anything. We’re not supposed to be machines that spit out solutions on this forum. I am not paid to do this.
Btw, I/we have told you everything you’re supposed to know. GO LEARN C#. There is a vast sea of tutorials and Microsoft’s own knowledge compendiums out there. FOR FREE.
1 Like
If you’re looking for the index of the variable rand, there’s a method for that 
Check the arguments/parameters that methods require, RemoveAt() needs an int, so you need to determine the index first, or you can do it in the argument itself (nesting methods).