Hi, I’m hoping someong can help me out with this problem I’m having:
I’m trying to display a random shape (which is a UI Image) and make it a random colour. I have 5 random shapes in my shapes array and 5 random colours in my colour array (for example, display a red square, or blue circle). I have a separate script for the shape colours so that I can match the colour word to the colour of the actual shape.
Currently, when I run my game, I can see that a shape of a random colour has been set, but the image is not displaying on the screen (see screenshot). When I remove the code that sets the shape colour, a random shape is displayed on the screen (white). I am not sure why I can’t get a coloured image to be displayed. Any help is greatly appreciated!!
public class ShapeColours : MonoBehaviour
{
public Color shapeColour;
public string shapeColourName;
}
public class ColourLevelManager : MonoBehaviour
{
//array of shape colours and shapes
[SerializeField] ShapeColours[] shapeColours;
public Sprite[] shapes;
//array of colour words
[SerializeField] public string[] colourWord;
public Text targetText;
public Image stimulusShape;
private void Start()
{
LevelOneNewColour();
}
public void LevelOneNewColour()
{
//random target colour word:
int randomWord = Random.Range(0, colourWord.Length);
targetText.text = colourWord[randomWord];
//random shape
int randomShape = Random.Range(0, shapes.Length);
stimulusShape.sprite = shapes[randomShape];
//random shape colour
int randomShapeColour = Random.Range(0, shapeColours.Length);
stimulusShape.color = shapeColours[randomShapeColour].shapeColour;
}
}
In your second screenshot, your (orange) colour seems to have an alpha value of 0 (the black bar under your colour in the Inspector), which means that your image has become completely transparent. So definitely check how these colours are generated/set (particularly the alpha).
You can try this :
int randomShapeColour = Random.Range(0, shapeColours.Length);
Color colorRef = shapeColours[randomShapeColour].shapeColour;
stimulusShape.color = new Color(colorRef.r, colorRef.g, colorRef.b, 1f);