-1
\$\begingroup\$
public List<SpriteRenderer> spritebox = new List<SpriteRenderer>();
public List<Color> colorbox = new List<Color>();

void Start() {
    var renderer = GetComponent<SpriteRenderer>();
    colorbox.Add(renderer.color);//I did so, except that it sends the color of the sprite to the list and in the last slot

    spritebox = GetComponent<SpriteRenderer>();
    colorbox = spritebox.color;//I did that too but it did not work

}

I'm trying to make the first color of the list in case the element 0 send the color to the sprite,would have any tips to help me?

enter image description here

\$\endgroup\$
0
\$\begingroup\$

In arrays and C# lists (which are wrappers for arrays that allow dynamic resizing) you should access elements by index in square brackets.

Examples:

This will get the first color from colorbox and store it in variable color:
Color color = colorbox[0];

This will assign color color to the first SpriteRenderer from spritebox:
spritebox[0].color = color;

You can put a variable or an expression inside brackets.

\$\endgroup\$
  • \$\begingroup\$ Thank you it worked perfectly \$\endgroup\$ – Rangel Jun 13 '19 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.