• 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 NerdRageStudios · Nov 13, 2014 at 12:36 PM · materiallerp

Problem using lerp and material alpha to fade out a gameObject when enemy is killed

Hi, I am trying to make the object fade out when its killed, however all it does is just disappear when the destroy is called. Please can someone figure out where I have gone wrong trying to lerp the alpha channel, I thought I had done this correctly!

I cant see the wood for the trees anymore ;)

    void Dead()
     {
         if (audio.isPlaying) return;
         audio.PlayOneShot(enemyDeathSounds[Random.Range(0, enemyDeathSounds.Length)]);
         enemyAlive = false;
         anim.enabled = false;
         movement.enabled = false;
         hearing.SetActive(false);
         sight.SetActive(false);
         melee.SetActive(false);
         nav.enabled = false;
         StartCoroutine(DestroyCorpse());
     }
 
     IEnumerator DestroyCorpse()
     {
             yield return new WaitForSeconds(corpseFadeDelay);
             StartCoroutine(FadeTo(1.0f, 2.0f));
             Destroy(this.gameObject);    
     }
 
     IEnumerator FadeTo(float fadeValue, float aTime)
     {
         float alpha = model.renderer.material.color.a;
         for(float t = 0.0f; t < 1.0f; t +=Time.deltaTime / aTime)
         {
             Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,fadeValue, t));
             model.renderer.material.color = newColor;
             yield return null;
         }
         
     }

Any help would be greatly appreciated :) Oh and if my code is badly written, bear with me I am still learning!

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

Answer by GameVortex · Nov 13, 2014 at 12:55 PM

The Destroy function is not part of the CoRoutine which means that it will be called immediately after you call start the Coroutine and the object will be destroyed at the end of the first frame. The easiest solution is to include the fadetime as the delay parameter in the destroy function:

 Destroy(gameObject, 2.0f);

At line 18 you are calling FadeTo(1.0f, 2.0f), where the first parameter is the fade value and you have set it to 1.0f. This means that the FadeTo function will fade the alpha to 1.0f which is fully opaque. You probably want to use 0.0f for the fade value so it fades to fully transparent instead.

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 NerdRageStudios · Nov 13, 2014 at 05:49 PM 0
Share

Doh! that was the problem, I set it to 1.0 not 0.0! I also moved the destroy function and it works fine :)

Wood... Trees!

Thanks GameVortex and Baste :D

avatar image
1

Answer by Baste · Nov 13, 2014 at 12:54 PM

It's pretty clear what's happening. You start off the coroutine that fades out the material, and then immediately destroy the object:

 StartCoroutine(FadeTo(1.0f, 2.0f)); //This starts to fade out the object
 Destroy(this.gameObject); //This destroys the object immediately


What you probably want to do is to move your Destroy-call to the end of the FadeTo method. If the fadeDelay is supposed to be a wait between when fading starts and when the destroy is called, you just need to reorder your DestroyCorpse Coroutine to this:

 StartCoroutine(FadeTo(1.0f, 2.0f));
 yield return new WaitForSeconds(corpseFadeDelay);
 Destroy(this.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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

lerp in shader doesn't work as imagined? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

How can I blend between 5 different materials? 2 Answers

Changing two different objects renderer colour 1 Answer

Lerp sets value instantly, not interpolating it? 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