- Home /
Trouble displaying random sprite of random colour
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;
}
}
Answer by Dragate · Jul 17, 2019 at 07:26 AM
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).
How do I go about changing the alpha value to make the image opaque? Is there an option in the inspector? Or do I do this within my script - I'm assu$$anonymous$$g that I need to set the alpha value to 1 to make it not transparent but I'm not sure how to access this alpha value. Thanks again in advance for your help!
I've created my colour variable and colour name variable (e.g., blue colour, and "blue" colour name) in the ShapeColours script. I then created a ShapeColours class array in the ColourLevel$$anonymous$$anager script which I dragged onto empty gameObjects if you can see in the picture under ShapeObjects (Shape Blue, Shape Green, etc.) For each colour, I just used the inspector to choose my colours using the colour wheel. I'm not sure where the alpha/transparency option is.
Take reference to selected color, and Assign color as a new Color(ref.r, ref.g, ref.b, 1f).
Answer by LetsTryItNow · Jul 17, 2019 at 08:42 AM
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);