• 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 Subliminalman · Feb 04, 2014 at 07:56 PM · colorruntimesprite renderer

Sprite not updating color.

Hey all,

I'm trying to set a sprite's color at runtime. I believe my code is correct but it is not currently working. The Debug statement returns the values I expect but the sprite is not updating. The script is attached to the sprite itself.

 using UnityEngine;
 using System.Collections;
 
 public class PuzzleBackground : MonoBehaviour {
     public SpriteRenderer spriteRenderer;
     Color [] colors = {new Color(54,196,152), new Color(114,213,127), new Color(219,128,183), 
                        new Color(127,201,255), new Color(237,237,128), new Color(166,153,222), 
                        new Color(242,143,86), new Color(103,208,213), new Color(51,167,179)};
     // Use this for initialization
     void Start () {
         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
         RandomColor ();
     }
 
     void Update(){
         if(Input.GetKey(KeyCode.Space)){
             RandomColor();
         }
     }
 
     public void RandomColor(){
         spriteRenderer.color = colors[Random.Range (0, colors.Length)];
         Debug.Log (spriteRenderer.color);
     }
 }



Any ideas?

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

1 Reply

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

Answer by Eric5h5 · Feb 04, 2014 at 08:29 PM

Color uses floats in the range of 0.0 to 1.0. All the colors you have will be the same (namely, white).

Comment
Add comment · Show 2 · 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 Subliminalman · Feb 04, 2014 at 08:50 PM 0
Share

Aghh I always forget that! Thanks.

avatar image Anxo · Jun 03, 2014 at 04:39 PM 0
Share

lol spent the last 20min trying to figure out what is going on. Thank you Eric.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Call and Render a Screenshot at Runtime,How do i render a sprite that was created at runtime (Screen Cap Question) 2 Answers

Setting a 2D sprite to a specific frame at runtime 3 Answers

Sprite Set Color to White? 0 Answers

Change material color at runtime!! 5 Answers

Changing a second objects material on trigger 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges