Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Rosscoe3 · Jun 25, 2015 at 01:15 AM · spritecolorspriterenderertintsetcolor

Adding Tint to a Sprite

Hi so i want to add a blue tint to a sprite that i have created.. every time this script runs i would like it to get bluer and bluer until health = 0.. making it all blue.. this is my current code:

 public class DestroyOnRainContact : MonoBehaviour
 {
     public Slider healthBar;
     public int Health;
     public GameObject Character;
 
     void Start()
     {
         Health = 100;
     }
 
     void OnCollisionEnter2D (Collision2D coll)
     {
         if(coll.gameObject.tag == "Rain")
         {
             //Destroy (coll.gameObject);
             Health -= 10;
         }
         if (Health == 0) 
         {
             Destroy(gameObject);
         }
     }
 
     void Update ()
     {
         if (healthBar.name == "Slider") 
         {
             healthBar.value = Health;
         }
     }
 }

Help would be awesome!! thanks :D

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
2
Best Answer

Answer by maccabbe · Jun 25, 2015 at 05:08 AM

Seems like you want to edit the color of the sprite renderer renderer. Get the renderer using GameObject.GetComponent(). From there change the color of the material using SpriteRenderer.color. Since you want your object to get bluer you probably want to start with pure white and go to pure blue, i.e. Color(health/100, health/100, 1, 1) would change the color linearly from white (1, 1, 1) at 100 health to blue (0, 0, 1) at 0 health.

http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

http://docs.unity3d.com/ScriptReference/SpriteRenderer.html

  void OnCollisionEnter2D (Collision2D coll)
  {
      if(coll.gameObject.tag == "Rain")
      {
          //Destroy (coll.gameObject);
          Health -= 10;
          gameObject.GetComponent<SpriteRenderer>().color=new Color(health/100f, health/100f, 1, 1);
      }
      if (Health == 0) 
      {
          Destroy(gameObject);
      }
  }
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
avatar image
0

Answer by Rosscoe3 · Jun 25, 2015 at 05:18 AM

Yay that is working pretty well!!! although i am running into this problem.. Some of the Rain particles are turning that color as they collide with themselves :P alt text


screen-shot-2015-06-25-at-11717-am.png (21.0 kB)
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 maccabbe · Jun 25, 2015 at 05:22 AM 1
Share

You can disable collisions using the Physics settings. For instance, to disable rain/rain collisions make a new layer "Rain". Then go to the Physics2DSettings (edit->Physics2DSettings) and uncheck Rain x Rain in the Layer collision matrix. Finally, change the layer of the rain objects to Rain.

avatar image Rosscoe3 · Jun 26, 2015 at 08:51 PM 0
Share

that sort of works.. the rain doesn't turn blue.. but will not collide with any other objects.. and dissapears on contact.. how do i fix this?alt text

screen-shot-2015-06-26-at-43807-pm.png (209.7 kB)

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

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

How to access SpriteRenderer Color from a shader? 1 Answer

How to get Sprite color palette 1 Answer

Set Gameobject a certain color based off of other game objects 1 Answer

Erase certain color from sprite 0 Answers

Problem with a color randomization 2 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