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
0
Question by Clastic · Oct 27, 2014 at 10:42 PM · spritetransparencyalpha

Why isn't my script changing the sprite's transparency?

Good evening, fellow developers,

basically I have a script for changing the transparency of an object but it is not working on a new object that I have attached it to (although it was working just fine for a previous object). The code is below

  #pragma strict
 var sprite : Renderer;
 var i : float;
 var rate : float;
 var trans : boolean;
 
 function Start () {
 i = 1;
 sprite = gameObject.GetComponent("Renderer");
 }
 
 function Awake () {
 i = 1;
 sprite = gameObject.GetComponent("Renderer");
 }
 
 function Update () 
 {
     if (trans == false)
     {
     sprite.material.color = new Color(1, 1, 1, i);
     i -= rate;
     if (i < 0)
     {
         trans = true;
     }
     }
     
     if (trans == true)
     {
     sprite.material.color = new Color(1, 1, 1, i);
     i += rate;
     if (i > 1)
     {
         trans = false;
     }
     }
 }

This is the modified code for my second object:

 #pragma strict
 var blackout : boolean;
 var sprite : Renderer;
 var i : float;
 var rate : float;
 
 function Start ()
 {
     i = 0;
     sprite = gameObject.GetComponent("Renderer");
 }
 
 function Update ()
 {
     if (blackout == true)
     {
         FullBlackOut();
     }
 }
 
 function StartBlackout()
 {
     blackout = true;
 }
 
 function FullBlackOut()
 {
     sprite.material.color = new Color(1, 1, 1, i);
     i += rate;
     if (i > 1)
     {
         blackout = false;
     }
 }


Before I forget, it is worth mentioning that my first script is to make an object appear to be blinking whereas in my second script the object is a black sprite that covers most of the screen and the alpha layer is preset to zero. Therefore in the second script I am trying to raise the alpha layer so that the screen becomes dark for the player. What am I doing wrong? I've tried googling my problem and have not found a solution that works (most solutions or just rearranged versions of the code I currently have but I tried them anyways) and looking at the script it looks right. Is there a setting in the Unity Editor that I have wrong?

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
1

Answer by Eric5h5 · Oct 27, 2014 at 11:13 PM

You should use GetComponent(SpriteRenderer).color rather than the material. Also, there's no reason to have the same code in Start and Awake (get rid of one of those), don't use strings with GetComponent, and your code won't work right because it's framerate-dependent; you need to use Time.deltaTime. Since you're using Unityscript there's no point writing out the entire color when changing the alpha value; just do GetComponent(SpriteRenderer).color.a = .5 (or whatever).

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

28 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

Related Questions

Transparency on overlapping 2D objects 0 Answers

Alpha not displaying when changed in code, unless changed manually in the inspector 0 Answers

Why does adding a point light make my sprite transparent? How can I add light but keep the sprite opaque? 1 Answer

Fading in/out SVGs without showing the half-transparent SVG-layers 1 Answer

Alpha blended particle trimming 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