Search Unity

How to Start color list

Discussion in 'Scripting' started by carnag45, Jun 13, 2019.

  1. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    Code (CSharp):
    1.     public List<SpriteRenderer> spritebox = new List<SpriteRenderer>();
    2.     public List<Color> colorbox = new List<Color>();
    3.  
    4.     void Start() {
    5.         Color color = colorbox[0];
    6.         spritebox.[0].color = color;
    7.     }
     
    Last edited: Jun 19, 2019
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    As per the error message, you're trying to assign single values to a List. Use the List's Add method instead;
    Code (CSharp):
    1. var renderer = GetComponent<SpriteRenderer>();
    2. spritebox.Add(renderer);
    3. colorbox.Add(renderer.color);
     
  3. carnag45

    carnag45

    Joined:
    May 22, 2019
    Posts:
    25
    did not work very well because I'm trying to set the color for the sprite. and not from the sprite to the list
    I did this in a different way, but this error appears.
     
  4. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Well, it's the solution to your original question but you've since edited your post and replaced it with a whole different problem. The error you're now seeing is due to empty Lists.

    If your intention is to assign values in the inspector then remove the initialisers to prevent your fields being overwritten when you enter playmode;
    Code (CSharp):
    1. public List<SpriteRenderer> spritebox;
    2. public List<Color> colorbox;
    3.  
    4. void Start() {
    5.     Color color = colorbox[0];
    6.     spritebox.[0].color = color;
    7. }