Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Random colours from a list

Discussion in 'Scripting' started by thewonderingvagabond, Apr 22, 2023.

  1. thewonderingvagabond

    thewonderingvagabond

    Joined:
    Mar 11, 2023
    Posts:
    16
    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?
    Code (CSharp):
    1.     private SpriteRenderer sr;
    2.     public List<Color[]> coloursList = new List<Color[]>();
    3.  
    4. void Start()
    5.     {
    6.         Color[] array1 = new [] { Color.red, Color.blue, Color.green };
    7.         coloursList.Add(array1);
    8.         sr = GetComponent<SpriteRenderer>();
    9.         int rand = Random.Range(0, coloursList.Count);
    10.         sr.material.color = coloursList[rand];
    11.     }
    12.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    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:
    Code (CSharp):
    1.     private SpriteRenderer sr;
    2.     public List<Color> coloursList;
    3.  
    4.     void Start()
    5.     {
    6.         coloursList = new List<Color>() { Color.red, Color.blue, Color.green };
    7.  
    8.         sr = GetComponent<SpriteRenderer>();
    9.         int rand = Random.Range(0, coloursList.Count);
    10.         sr.material.color = coloursList[rand];
    11.     }
    ... or ...
    Code (CSharp):
    1.     private SpriteRenderer sr;
    2.     public List<Color> coloursList = new List<Color>();
    3.  
    4.     void Start()
    5.     {
    6.         coloursList.Add(Color.red);
    7.         coloursList.Add(Color.blue);
    8.         coloursList.Add(Color.green);
    9.  
    10.         sr = GetComponent<SpriteRenderer>();
    11.         int rand = Random.Range(0, coloursList.Count);
    12.         sr.material.color = coloursList[rand];
    13.     }
     
    thewonderingvagabond and Bunny83 like this.
  3. thewonderingvagabond

    thewonderingvagabond

    Joined:
    Mar 11, 2023
    Posts:
    16
    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...
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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.
     
  5. thewonderingvagabond

    thewonderingvagabond

    Joined:
    Mar 11, 2023
    Posts:
    16
    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/d...#System_Collections_Generic_List_1_Remove__0_
     
  6. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    285
    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<T>.
    Hint: Anything else starting with "Remove"
     
  7. thewonderingvagabond

    thewonderingvagabond

    Joined:
    Mar 11, 2023
    Posts:
    16
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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.
     
    icauroboros likes this.
  9. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    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).