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 Bentoon · Sep 28, 2016 at 11:39 PM · c#timerfadealpha-channel

Fading Object in and out with timer

Hello,

I have a fast scene that plays and with objects I want to fade in and out at different times...

Simple Right..?

But I'm missing it . Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class timer : MonoBehaviour {
 
 
     public string next;
     public float time = 8.0f;
     public float time2= 26.0f;
     public float time3= 45.0f;
 
     public GameObject One;
     public GameObject Two;
 
     public float oneAlpha = 0.0f;
     public float twoAlpha = 0.0f;
 
 
     // Use this for initialization
     void Start () {
         StartCoroutine("Wait");
     }
     
 
 
  
 
 
 
 IEnumerator Wait()
     {
         if(WaitForSeconds(time))
         {
             One.GetComponent<Renderer>().material.color = new Vector4(0, 0, 0, oneAlpha);
         }
 
         if (WaitForSeconds(time2))
         {
             Two.GetComponent<Renderer>().material.color = new Vector4(0, 0, 0, twoAlpha);
         }
 
         if (WaitForSeconds(time3))
         {
                 Application.LoadLevel(next);
         }
            
     }    
     
 }
 
Comment
Add comment · Show 2
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 TBruce · Sep 29, 2016 at 12:43 AM 0
Share

What exactly is it do you want to do with these two object? Do you want to fade each one completely out individually one after the other? Your code is not clear?

avatar image Bonfire-Boy · Sep 29, 2016 at 03:16 AM 0
Share

@Bentoon You should have told us what the problem is (eg the error you're being given on trying to compile this).

Fortunately one problem at least is easy to spot. You're not using WaitForSeconds properly. To wait for n seconds just put

yield return new WaitForSeconds(n);

See https://docs.unity3d.com/ScriptReference/WaitForSeconds.html. In particular, "WaitForSeconds can only be used with a yield statement in coroutines."

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toddisarockstar · Sep 29, 2016 at 01:22 AM

 public class NewBehaviourScript : MonoBehaviour {
     public GameObject dude;//here is your object
     public float alpha;
     public bool sw;
     public Color dudescolor;
     // Use this for initialization
     void Start () {
         //must have a transparent shader To work !!!
         dude.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
             //lets remember the color we started with!!!!
         dudescolor =dude.GetComponent<Renderer>().material.color;
 
     }
     
 
     void Update () {
             //play with our float a little bit
         if (sw){alpha+=Time.deltaTime;
             if(alpha>1){sw=!sw;}}
         if(!sw){alpha+=-Time.deltaTime;
             if(alpha<0){sw=!sw;}}

             //assign the float to your gameobjects alpha!!!
         dudescolor.a = alpha;
         dude.GetComponent<Renderer>().material.color = dudescolor;
     }
 }
 

this is a working example of fading color! im not sure when or how you want to change it but this should include example of everything you need!!! Void update changes ever frame. so use it to change a float a little bit every frame and apply it to a the alpha channel of your color. dont use vector4. use a Color variable instead. and color.a is the alpha channel you are looking for!

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 Bentoon · Sep 29, 2016 at 03:04 AM 0
Share

@toddisarockstar Thanks Rockstar!

a few questions:

one is what am I doing wrong with the IENumerator to be able to call specific time when the Fade would be triggered

The Second question is I am using models with sub meshes so even if they the shader is set to Trans /or Fade texture so not Lerping like a primitive would because it doesnt aply to children. I hav tried things like Simple LOD to merge the mesh but it doesn't work...Any obvious problemthat I'm not seeing

THAN$$anonymous$$ YOU !

avatar image toddisarockstar · Sep 29, 2016 at 04:08 AM 0
Share
 public class NewBehaviourScript : $$anonymous$$onoBehaviour {
     public GameObject dude;
     public List<Transform> dudeparts = new List<Transform>();
 
 
     public float alpha;
     public bool sw;
     public Color dudescolor;
     public bool fade;
 
 
     // Use this for initialization
     void Start () {
         dudeparts.Add(dude.transform);
         foreach (Transform tr in dude.transform) {
                         print (tr.name);
                         dudeparts.Add (tr);
                         tr.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
                         foreach (Transform tr2 in tr) {
                                 dudeparts.Add (tr2);
                                 tr2.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
                         }
                 }
         //must have a transparent shader To work !!!
         dude.GetComponent<Renderer>().material.shader=Shader.Find("Transparent/Diffuse");
         //lets remember the color we started with!!!!
         dudescolor =dude.GetComponent<Renderer>().material.color;
         
     }
     
     
     void Update () {
         if(Input.Get$$anonymous$$eyDown("space")){whateverfunction();}
 
         //play with our float a little bit
         if (sw){if(alpha<1){alpha+=Time.deltaTime;
             }}
         if (!sw) {
                         if (alpha > 0) {
                                 alpha += -Time.deltaTime;
             }}
         //assign the float to your gameobjects alpha!!!
         dudescolor.a = alpha;
         foreach(Transform tr in dudeparts){
             tr.GetComponent<Renderer>().material.color = dudescolor;}
     }
 
     void whateverfunction(){
         sw = !sw;
     }
 }

this code would also include the children of the object and those children's children. it's a loop inside of a loop to find the kiddoes.

im not sure why you would need IEnumerator in unity. but here is an example of calling a function when the space bar is pressed. it doesn't matter where or how you change the sw variable in this script. it sill simply fade in or out however or wherever you change the sw variable.

if i want to count off something in time, i personally use Time.deltatime in update. i started on game development and it works a bit different than other types of program$$anonymous$$g. things rely on time as much or more that events. and when thing are triggered, time is also very important to know the order that those things where triggered. the update function is your main tool here in unity! it updates once every visual frame and allows you too keep track of everything in order of when they happen and keeps everything in sync. does that make sence?

for example.... if one object somewhere is destroyed somewhere in the scene or the reference is null even a microsecond before another script calls it, it produces an error and youre crashed!

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

227 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Timer Between Labels 2 Answers

Making a fill take exactly n seconds to complete 2 Answers

More timer from same class 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