- Home /
How do I cycle through colors in an array, setting the selected color as the colour for an image?
Hi. Firstly, thanks so much to whoever helps.
I am creating an app that lets you train for different sports at home and revolves around drills and the matching colours on the app. In the settings, the player can select a colour to use. The way I would like it to work is that it cycles through the array of colours when you click on the colour in settings. My question is, how do I get the colours to cycles through the array, setting the current colour to the variable colour1?
I have attached some photos.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColourPicker : MonoBehaviour
{
public Color[] colourPalette;
private Color currentSelectedColour;
public Settings settings;
private Color colour1;
void Start()
{
colour1 = settings.colour1;
}
public void ChangeColourOne()][1]
{
// Cycle through to the next colour in the array and Set as colour1
}
}
Answer by Hellium · Apr 08 at 11:36 AM
public class ColourPicker : MonoBehaviour
{
public Color[] colourPalette;
private Color currentSelectedColour;
public Settings settings;
private Color colour1;
private int selectedColourIndex = -1;
void Start()
{
colour1 = settings.colour1;
}
public void ChangeColourOne()][1]
{
if(colourPalette.Length == 0) return;
selectedColourIndex = (selectedColourIndex + 1) % colourPalette.Length;
colour1 = colourPalette[selectedColourIndex];
}
}
When I try to use your code, all the ChangeColourOne void goes red with an error. The errors go away when I delete ][1] which is after the brackets on the void but the code still doesn't work.
Do you know what might be wrong? Thanks
I blindly copy pasted your code which had this ][1]
Please, define "does not work", it works perfectly fine on my side. colour1
gets changed.
Because Color is a struct, chaging colour1 won't affect
settings.colour1`
Thanks so much. I got it working and it's awesome.
Your answer
