0
\$\begingroup\$

I can already make one for (int) normally, but I'm trying to create a sprite renderer loop using (i ++ / i--) sorry, I do not know the names for this condition but it's showing a mountain of errors

public List<Color> Sprit;


public void Previous(){
for(int i = 0; i Sprit.Count; i++)
{    
    Sprit[i].GetComponent<SpriteRenderer>().color = Sprit;
}
public void Next(){
for(int i = 0; i< Sprit.Count; i--)
{    
    Sprit[i].GetComponent<SpriteRenderer>().color = Sprit;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It is very unclear to me what you are trying to do here. Please note that Sprit is a List<Color, and that Sprit[i].GetComponent<SpriteRenderer>().color is a Color, so your assignment cannot work. \$\endgroup\$ – Eldy Apr 2 '19 at 7:41
  • \$\begingroup\$ It looks to me like you might want to work through some basic C# tutorials to get a better grasp of the language's fundamentals. \$\endgroup\$ – DMGregory Apr 2 '19 at 11:43
0
\$\begingroup\$

While your question asks for help with loops, it looks as though actually all you want to do is select the next or previous colour in a list, and apply that to a list of re-determined colours.

To do that we must declare the list of colours, the index for those colours, and the SpriteRenderer.

We then increment or decrement the index, based on the selection, and then change the Colour in the SpriteRenderer accordingly. It doesn't require looping.

public List<Color> coloursList = new List<Color>(); // Fill this list in the editor with colour values
public int colour = 0; // The colour to start from
private SpriteRenderer sR; /* The sprite renderer itself. Attach this script   
to the gameobject you want to change the colour of */

private void Start(){
sR = gameObject.GetComponent<SpriteRenderer>(); // Get the sprite renderer
}


public void Previous(){
colour--;
if(colour < 0){colour = coloursList.Count - 1;}   
sR.color = coloursList[colour];
}

public void Next(){
colour++;
if(colour >= coloursList.Count){colour = 0;}
sR.color = coloursList[colour];
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for your explanations, but there is an error, I am defining a list along with the color list, but gives error index, for example, I have 10 spritesrendere in the array with 5 color components. Can you tell me what's wrong? I put it so sR[color] .color = colorsList [color]; even setting another int happens the error. \$\endgroup\$ – Nitecki Apr 2 '19 at 21:49
0
\$\begingroup\$

For the sake of giving this old question a more complete answer:

It looks like you really want two separate collections:

  • A collection of renderers whose colours you want to change
  • A collection of colours to change them to

We can do this with two arrays (or lists, if you need to add/remove items dynamically):

public class SpriteColorChanger : MonoBehaviour {

    public SpriteRenderer[] spriteRenderers;

    // I give this a default value so the script is well-behaved,
    // even if we forget to populate the color array in the inspector.
    public Color[] colors = new Color[]{ new Color(1, 1, 1, 1) };

    int _currentColorIndex;

    void UpdateAllRenderers() {
        var currentColor = colors[_currentColorIndex];

        foreach(var renderer in spriteRenderers)
            renderer.color = currentColor;
    }

    void NextColor() {
        // The % operator, "modulo", will wrap the index around, 
        // so if we go past the end of our array, we restart at 0.
        _currentColorIndex = (_currentColorIndex + 1) % colors.Length;

        UpdateAllRenderers();
    }

    void PreviousColor() {
        // (-1 % length = -1), but we want to wrap around to (length - 1),
        // so we add instead of subtracting, to wrap below zero correctly.
        _currentColorIndex = (_currentColorIndex - 1 + colors.Length) % colors.Length;

        UpdateAllRenderers();
    }

    void Start() {
        UpdateAllRenderers();
    }
}
\$\endgroup\$

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.