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 /
  • Help Room /
avatar image
0
Question by Mechanical Paladin · Dec 14, 2015 at 01:21 PM · c#colorsprite renderer

Get sprite color r,g,b,a, values c#

How do I access sprite's color values? (line 16)

 using UnityEngine;
 using System.Collections;
  
 public class scr_spriteFade : MonoBehaviour {
  
         private float orgR;
         private float orgG;
         private float orgB;
         private float orgA;
  
         public float alphaAmount = 63.75f;
         public float fadeTime = 1.0f;
  
         // Use this for initialization
         void Start () {
                 Color32 orgColor = /*somehow get the original r,g,b,a channel values and store them in "orgColor" */ ;
  
                 orgR = orgColor.r;
                 orgG = orgColor.g;
                 orgB = orgColor.b;
                 orgA = orgColor.a;
         }
 }

Like when you want transform's values, you'd use something like:

 Vector3 orgPos = transform.localPosition;

Is there a similar way to access the color values?

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
0

Answer by Dave-Carlile · Dec 14, 2015 at 01:23 PM

If you take a look at the Sprite documentation you'll see that it refers to the SpriteRenderer as the component that is responsible for displaying the sprite graphic. Looking at that component you'll see that it has a color property.

So given your sprite game object you need to use GetComponent to get a reference to the SpriteRenderer, and that will give you access to the color property.

Comment
Add comment · Show 4 · 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 Mechanical Paladin · Dec 14, 2015 at 01:40 PM 0
Share

$$anonymous$$ay I ask for an example code for how to get current color values?

avatar image Dave-Carlile Mechanical Paladin · Dec 14, 2015 at 01:52 PM 0
Share

It's just a property on the sprite renderer component...

 Color color = renderer.color;
avatar image Mechanical Paladin Dave-Carlile · Dec 14, 2015 at 02:09 PM 0
Share
     void Start () {
         gameObject.GetComponent<SpriteRenderer> ();
         Color color = renderer.color;
     }

does not work.

 Assets/scripts/environment/scr_spriteFade.cs(17,31): error CS0619: `UnityEngine.Component.renderer' is obsolete: `Property renderer has been deprecated. Use GetComponent<Renderer>() ins$$anonymous$$d. (UnityUpgradable)'

 Assets/scripts/environment/scr_spriteFade.cs(17,40): error CS1061: Type `UnityEngine.Component' does not contain a definition for `color' and no extension method `color' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Show more comments
avatar image
0

Answer by wibble82 · Dec 14, 2015 at 02:11 PM

Try this: using UnityEngine; using System.Collections;

  public class scr_spriteFade : MonoBehaviour {
   
          private Color original_color;
   
          public float alphaAmount = 63.75f;
          public float fadeTime = 1.0f;
   
          // Use this for initialization
          void Start () {
                  original_color = GetComponent<SpriteRenderer>().color;
          }
  }

So I'm getting the sprite renderer component in 'start', and storing the color. Note that I'm not storing the initial value as separate floats - its a color, so I'm storing it as a color!

-Chris

Comment
Add comment · Show 4 · 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 Mechanical Paladin · Dec 14, 2015 at 02:17 PM 0
Share

That's the thing. I need to store the values separately.

avatar image wibble82 Mechanical Paladin · Dec 14, 2015 at 02:25 PM 0
Share

Well you can, but I would question whether you need to. The values can be accessed on 'original_color' easily:

 //print out the red bit of original color
 Debug.Log(original_color.r)
 
 //add 0.1 to the original color
 original_color.r += 0.1f;
 
 //create a new color which is the original tweaked a bit!
 Color new_color = original_color;
 new_color.r += 0.1f;
 new_color.g = 0.0f;

But either way, the first line is key - it reads the color value out of the sprite renderer. Whether you choose to store the r/g/b/a values separately or not is up to you!

As a general piece of advice, good practice when program$$anonymous$$g is store data in a way that represents what it means! So if you have a set of numbers that represent a color, store them as a color. This makes your code easier to read, and means that all of unity's 'color' related functions will work with it.

Obviously you can store the red/green/blue/alpha values separately, but the is no gain. The 'color' structure stores 4 floats internally that you can access easily.

If you're confused, let me know what you're trying to do and I'll see if I can help explain why storing the numbers separately is unnecesary.

-Chris

avatar image Mechanical Paladin wibble82 · Dec 14, 2015 at 04:05 PM 0
Share

Somehow line

 sprRenderer.color = new Color32 (orgR, orgG, orgB, alphaAmount);

doesnt work. If I input values between 0-255 it works but orgR/G/B/A doesn't, even though they are floats.

Here is the current code: using UnityEngine; using System.Collections;

 public class scr_spriteFade : $$anonymous$$onoBehaviour {
 
     private Color32 originalColor;
     private float orgR;
     private float orgG;
     private float orgB;
     private float orgA;
 
     public float alphaAmount = 63.75f;
     public float fadeTime = 1.0f;
 
     // Use this for initialization
     void Start () {
         originalColor = GetComponent<SpriteRenderer> ().color;
         orgR = originalColor.r;
         orgG = originalColor.g;
         orgB = originalColor.b;
         orgA = originalColor.a;
     }
 
 
     void OnTriggerEnter2D(Collider2D collider) {
         SpriteRenderer sprRenderer = GetComponent<SpriteRenderer>();
         sprRenderer.color = new Color32 (orgR, orgG, orgB, alphaAmount);
     }
         
     void OnTriggerExit2D(Collider2D collider) {
     }
 
 }
 
Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Change color of a pixel to either black or white depending on how grey the pixel is 1 Answer

How to make RANDOM COLOR on shader 1 Answer

Is there any way to make a gradient within unity?,Is there any way to make a gradient? 1 Answer

Color.lerp for Spriterender is not smooth 1 Answer

How do I change color with Button onClick() 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