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 Finjo · Dec 03, 2017 at 06:23 PM · colorsprite renderer

Sprite alpha colour not changing via script

I'm a bit of a Unity novice so apologies in advance if I'm missing something really obvious here.

I'm trying to change the alpha of one of my sprites on a key press - if I Debug.Log the alpha of that sprite on the key press it shows the value changing from 0 to 255 (or vice versa on a second key press), but in play mode nothing is changing on the Sprite Renderer itself.

Here are the relevant pieces of code:

 private Color redShip;
     
     void Start () {
         redShip = GameObject.Find ("RedShip").GetComponent<SpriteRenderer> ().color;
     }
 
     void Update(){
         if (Input.GetKeyDown (KeyCode.E)) {
             FlipShip ();
         }
     }
 
     void FlipShip(){
         print (redShip.a); // Will correctly show the current alpha
 
         if (redShip.a == 0) {
             redShip.a = Mathf.Lerp (0, 255, 1);
         } else {
             redShip.a = Mathf.Lerp (255, 0, 1);
         }
 
         print (redShip.a); // Correctly shows the updated alpha, but not visible in Unity itself
     }

I've also tried setting it to a temp variable as a full colour and then setting that back to the original Sprite Renderer value, but that didn't seem to make a difference.

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

Answer by Finjo · Dec 03, 2017 at 08:01 PM

Yep I was being stupid :) I needed to define the variable as a SpriteRenderer instead of a Color. Looks like the value was just being set to a random variable in my code without actually attaching to the SpriteRenderer.

Working code:

 private SpriteRenderer redShip;
 
 void Start () {
     redShip = GameObject.Find ("RedShip").GetComponent<SpriteRenderer> ();
 }

 void FlipShip(){
     Color tmp = redShip.color;

     if (tmp.a == 0) {
         tmp.a = Mathf.Lerp (0, 255, 1);
     } else {
         tmp.a = Mathf.Lerp (255, 0, 1);
     }

     redShip.color = tmp;
 }

The Lerp function isn't working as I intended it, but I'll spend some time looking into that.

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

71 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

Related Questions

What would cause a sprite or texture to be unable to tint its color? 1 Answer

How can I change the Value of a Color through script? 1 Answer

How to change color property of the Sprite Renderer in C# 1 Answer

i changed color of sprite still not changed 3 Answers

I need to store a value that can have "f" in it, but is in a float/ string 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