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 Zuon94 · Oct 22, 2014 at 03:21 PM · alphawaitforsecondsdelayfunction updatechannel

How to delay function Update execution?

I have a scene where a field of text is to fade in by raising the alpha channel over time. The effect works, but I would like to have it wait for a few seconds before it starts fading in. I know "yield.WaitForSeconds" doesn't work in the update function, and I've read online about Coroutines, but I don't really understand them. How could I incorporate this function into my code?

     private var myRenderer : Renderer;
     
     function Start () {
     myRenderer = renderer;
     yield WaitForSeconds(5);
     }
      
     function Update () {
     //yield WaitForSeconds(5);
     if (myRenderer.material.color.a < 255)
     myRenderer.material.color.a += 25.0*Time.deltaTime;
     }
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 infinitypbr · Oct 22, 2014 at 03:28 PM

I was also confused about coroutines, but is think the name is just confusing. The concept is quite simple.

In your update, call a function -- aka coroutine -- with a float variable like this:

 function Update(){
      MyFunction(0.5);
 }
 
 function MyFunction(delay : float){
      yield WaitForSeconds(delay);
      // enter your code here
 }

A coroutine is just another function. And other functions can happen over time, rather than right away.

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 infinitypbr · Oct 22, 2014 at 03:35 PM 0
Share

It's worth pointing out that you can pass many variables to a function:

 function Update(){
      BigFunction(0.5, "this is a string", gameObjectVariable, stringVariable, intVariable);
 }
 
 function BigFunction(delay : float, newString : String, myObject : GameObject, myString : String, myInt : int){
      // now try debug.log to print e data of all the variables passed into this function to e console
 }


This is helpful for passing variables to a function in another script.

avatar image Zuon94 · Oct 22, 2014 at 07:07 PM 0
Share

Thank you, I guess it's not so bad after all. This fixed my problem. c:

avatar image
0

Answer by robertbu · Oct 22, 2014 at 03:25 PM

The components of the Color class in unity go from 0.0 to 1.0, not 0 to 255. As for waiting, you can do it like this:

 function Start () {
      myRenderer = renderer;
      yield WaitForSeconds(5);
      while (myRenderer.material.color.a < 1.0) {
           myRenderer.material.color.a += Time.deltaTime / 2.0;
           yield;
      }
      myRenderer.material.color.a = 1.0;
 }
   

Assuming color.a starts at 0.0, this will take 2.0 seconds to fade it up once the 5 second wait is over.

Comment
Add comment · Show 3 · 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 Zuon94 · Oct 22, 2014 at 07:06 PM 0
Share

On the contrary, setting it to 1 only makes the alpha go up to 1. Setting it to 255 makes it appear completely. Also, if the alpha of the object is initially set at zero, no transition will occur at all.

Thank you very much for your advice, however!

avatar image robertbu · Oct 22, 2014 at 07:31 PM 0
Share

On the contrary, setting it to 1 only makes the alpha go up to 1 Setting it to 255 makes it appear completely.

No, you are absolutely wrong here. When color.a == 1.0, it is completely visible. From the reference:

Each color component is a floating point value with a range from 0 to 1.

http://docs.unity3d.com/ScriptReference/Color.html

Also, if the alpha of the object is initially set at zero, no transition will occur at all.

Assu$$anonymous$$g you are trying to bring this texture from not visible at all (alpha of 0) to fully visible (alpha of 1), then this code will take 2.0 seconds to do the job.

Did you try this code?

avatar image infinitypbr · Oct 22, 2014 at 07:39 PM 0
Share

In this type of code, the alpha & color values are a normalized float from 0.0 to 1.0. (Normalized, if I"m not mistaken, means it's a number that's reduced, through math, to a value between 0 and 1).

Your code could actually use a $$anonymous$$athf function (if you haven't checked those out yet, do -- they're really really REALLY handy and will save you lots of time).

 var curAlpha     : float     = 0.0;        // The current alpha value.  I keep it in a variable so I can use it on multiple things, such as a UI.Image and UI.Text, two different objects that will require the same alpha
 var desAlpha     : float     = 1.0;        // The desired alpha value.
 var speed$$anonymous$$od      : float      = 2.0;        // $$anonymous$$ultiplies by Time.deltaTime, so a value of 2 is double time, a value of 0.5 is half time (or two seconds)
 
 function Update(){
     if (curAlpha != desAlpha)
     {
         curAlpha    = $$anonymous$$athf.$$anonymous$$oveTowards(curAlpha, desAlpha, Time.deltaTime * speed$$anonymous$$od);
         myRenderer.material.color.a        = curAlpha;
     }
 }

In the code above, changing the value of desAlpha will change the value of the alpha on your material. This makes it easy to simply pass in a new value from other scripts as needed.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

check alpha of gameObject C# 3 Answers

WaitForSeconds, only pausing once 1 Answer

Make delay for spawn 3 Answers

How to Instantiate prefabs with function Update? 1 Answer

WaitForSeconds load level delay not working 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