Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lalalanni · Jul 17, 2019 at 05:35 AM · spritecolor

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;
     }
 }

alt text

alt text

shapedisplay.png (193.4 kB)
nodisplay.png (182.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lalalanni · Jul 17, 2019 at 07:35 AM 0
Share

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!

avatar image Dragate lalalanni · Jul 17, 2019 at 07:46 AM 0
Share

How have you created the different colours?

avatar image lalalanni Dragate · Jul 17, 2019 at 08:10 AM 0
Share

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.

Show more comments
avatar image LetsTryItNow lalalanni · Jul 17, 2019 at 08:05 AM 0
Share

Take reference to selected color, and Assign color as a new Color(ref.r, ref.g, ref.b, 1f).

avatar image
1

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get "pixel" color for Sprite (U4.3)? 2 Answers

Sprite Renderer - Tint Sprite? 0 Answers

How to change a SpriteRenderer's saturation 0 Answers

Instantiating multiple sprites and assigning different colors for each 1 Answer

How do I make a sprite flash green when tapped? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges