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 paco morales · Feb 08, 2013 at 04:47 PM · materialrenderervanish

Vanish one object with renderer.material?

Hello Everyone,

I'm trying to vanish one object using the function "renderer.material.color.a" using the mouse button to active it and re-active it.

I have some time trying but without success. I don't have experience in Java or Unity Script.

Please Any help is more than welcome.

Please take a look to my script.

     var duration : float = 1.5; // time taken to disappear
     private var alpha: float = 1; // alpha starts at 1
     private var vanish = false;
  
 
 
     function Update(){
     
     if (vanish){ // only decrement alpha after button pressed
     alpha -= Time.deltaTime/duration;
     if (Input.GetButton("Fire1")){
     disappear();
     if (Input.GetButtonUP("Fire1")){
     appear();
     }
     }
     }
  
     function disappear(){
     renderer.material.color.a  = alpha; // set alpha f 
     if (alpha > 0){ // only draw t  while it's visible 
     vanish = true; // enable alpha decrement
     }
     }
 
     function appear(){
     renderer.material.color.a  = alpha; // set alpha  
     if (alpha > 1){ // only draw t  while it's visible 
     vanish = true; // enable alpha decrement
     }
     }

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

Answer by Lockstep · Feb 08, 2013 at 05:31 PM

First of all if you want to use transparency you must make sure your shader can handle it. The default diffuse shader will never be transparent. Select a transparent shader from the drop down menu of the material.

About your code. It is a bit strange how you manipulate your alpha value. The alpha value is always between 0 and 1 so checking if your reference is greater 1 is quite fishy. You can use Mathf.Lerp to manipulate the alpha value in a smooth way. Try:

 var fadeSpeed : float = 10;
 function Update(){
 
     if(vanish){
         renderer.material.color.a = Mathf.Lerp(renderer.material.color.a, 0, Time.deltaTime * fadeSpeed);
     } else {
         renderer.material.color.a = Mathf.Lerp(renderer.material.color.a, 1, Time.deltaTime * fadeSpeed);
     }
     if(Input.GetButtonDown("Fire1")){      //You only need to check once not every frame like you did
         vanish = true;
     }
     if(Input.GetButtonUp("Fire1")){
         vanish = false;
     }
 }


The Mathf.Lerp gets things done in a smooth way. The first if checks if the button is pressed down and sets vanish to true. The second if does the opposit when the button is released. The GetButton you used would set the value every frame as long as the button is held down.

Comment
Add comment · Show 1 · 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 paco morales · Feb 08, 2013 at 05:49 PM 0
Share

Thanks $$anonymous$$an, now is working really good !!!

Thanks again for your help :)

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

10 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

Related Questions

Changing two different objects renderer colour 1 Answer

How to change the color using "renderer.material.color"? 2 Answers

A node in a childnode? 1 Answer

Changing material of objects by the tag in different scenes 0 Answers

How do i get texture from GUI box? 1 Answer


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