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 Trollvahkiin · Aug 25, 2013 at 06:21 PM · lighttimerintensity

Adding time before a script is initiated.

Hey,

So I have a script which makes the intensity of a light to go down over time. You can add wood to make it go back up but I want it to if it reaches intensity 2 to put a timer (yield WaitForSeconds) before the intensity goes back down.

 #pragma strict
 
 var addedWood : boolean = false;
 var lightStepDown : float = 0.01;
 var lightStepUp : float = 1;
 var maxIntensity : float = 1;
 var waitFor : int;
 
 function Update () 
 {
     light.intensity -= lightStepDown * Time.deltaTime;
 }
 
 function OnTriggerEnter (Col : Collider) 
 {
      if(Col.gameObject.tag == "Wood" && light.intensity <= maxIntensity)
      {
          yield WaitForSeconds(waitFor);
           addedWood = true;
           light.intensity += lightStepUp;
           yield WaitForSeconds(1);
           Destroy(Col.gameObject);
           yield WaitForSeconds(0.5);
           addedWood = false;
           
      }
      else if(light.intensity >= maxIntensity)
      {
          Debug.Log("Too much wood!");
          waitFor = waitFor + 10;
      }
 }

The WaitFor var does change to 10 but that doesn't stop the script from waiting.

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
0

Answer by fafase · Aug 25, 2013 at 06:28 PM

So you want the fire to go down constantly but if the player add a lot of wood the it waits for a while before decreasing.

 function Update ()
 {
    if(!FullFire())
       light.intensity -= lightStepDown * Time.deltaTime;
 }
 
 function FullFire()
 {
    if(waitFor > 0){
        timer -= Time.deltaTime;
        return true;
    }
    return false;
 }
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 fafase · Aug 25, 2013 at 06:28 PM 0
Share

Note, there may be other way, it is just this one seems simple to grasp.

avatar image Trollvahkiin · Aug 25, 2013 at 06:56 PM 0
Share

(30,28): UCE0001: ';' expected. Insert a semicolon at the end. (30,18): BCE0044: expecting (, found ':'.

That's is the errors I got but I'm not sure if I'm doing this right. I added the function update and then just make the function. Is that all I do?

avatar image fafase · Aug 26, 2013 at 07:43 AM 0
Share

Yes it is my function declaration I guess. Just remove the :Boolean. I never remember how this one works in UnityScript but it is not needed so you can take it off:

 function FullFire()
 {
    if(waitFor > 0){
        waitFor -= Time.deltaTime;
        return true;
    }
    return false;
 }
avatar image
0

Answer by DESTRUKTORR · Aug 25, 2013 at 06:38 PM

I'm assuming you want to force the player to wait a certain amount of time before adding more wood, and restrict how much wood can be on the fire, yes?

If so, simply track the last time wood was added and when you call this if statement:

 if(Col.gameObject.tag == "Wood" && light.intensity <= maxIntensity)

add a check against Time.time to see if it's been long enough since the last time wood was added to add more.

On a side note, however, you're checking for equality twice, here, and you make the player wait if -anything- enters the trigger field. I would suggest changing it to this:

 #pragma strict
  
 var addedWood : boolean = false;
 var lightStepDown : float = 0.01;
 var lightStepUp : float = 1;
 var maxIntensity : float = 1;
 var waitFor : int;
  
 function Update () 
 {
     if(waitFor > Time.deltaTime)
          waitFor -= Time.deltaTime;
     else if(waitFor != 0)// If you want it to add exactly 10 seconds, this is an important step.
          waitFor = 0;
     else
          light.intensity -= lightStepDown * Time.deltaTime;
 }
 
 function OnTriggerEnter (Col : Collider) 
 {
     if(Col.gameObject.tag == "Wood")
     {
          if(light.intensity + lightStepUp < maxIntensity)
          {
            addedWood = true;
            light.intensity += lightStepUp;
            Destroy(Col.gameObject, 1);
            addedWood = false;
          }
          else
          {
              lightIntensity = maxIntensity;
              Debug.Log("Too much wood!");
              waitFor += 10;
          }
     }
 }

[EDIT] Changed everything up to fit how you specified in the comment, better.

Comment
Add comment · Show 4 · 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 Trollvahkiin · Aug 25, 2013 at 06:50 PM 0
Share

I was thinking that if the max intensity is 2 and after adding the required amount of wood to match the max, then a 10sec wait would happened before the intensity started to drop again. Like fafase said.

avatar image DESTRUKTORR · Aug 25, 2013 at 06:56 PM 0
Share

Ah. That would require a bit of a change in your logic, then. I'll apply those edits to my answer :P

avatar image Trollvahkiin · Aug 25, 2013 at 07:02 PM 0
Share

That would be great. Thanks.

avatar image Trollvahkiin · Aug 25, 2013 at 07:16 PM 0
Share

It works..kind of. It reaches 2 and says Too $$anonymous$$uch wood and I can see for a spit seconds a number go down but it's not in seconds.

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Flashlight timer 2 Answers

Directional light - Help 0 Answers

Help make this timer work? 2 Answers

Cannot Get Light To Flash - Help 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