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 S1LB0T · Jun 10, 2013 at 11:18 PM · javascripteventrepeatingtimed

looping in javascript

I am currently working on a project that deals with in-game time. I have 3D models of buildings and am using a "glow" type texture/mesh to make the windows on them appear "lighted". I have a bit of code here:

   var lightGlow : MeshRenderer;

   function Update(){

   var seconds : int = Time.time;

     if(seconds < 5)
        renderer.enabled = true;
     else if(seconds > 5)
        renderer.enabled = false;

     if(seconds > 8)
        renderer.enabled = true;
     else if(seconds > 10)
        renderer.enabled = false;

     Debug.Log(seconds);}

I am trying to figure out how to make the seconds start over again after "x" amount, so that the lightglow variable is able to repeat continuously. I know there has to be a simple solution, but I've googled myself (almost) to death, and I've only been writing my own scripts for a little over a month now. Any help/tips would be greatly appreciated.

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

Answer by AmoralAckbar · Jun 11, 2013 at 04:08 PM

Time.Time is just going to keep counting up from the start of your game, so I don't think that you are going to be able to reset it.

This should do roughly what you want (my JS might have syntax issues as I use C#). It will flip the texture between enabled and disabled every 0.5 seconds. You can change the change rate to whatever you like. If you wanted a less static rate, you could update the code to have 2 different change rates that it flips between instead of just using the one.

 var lightGlow : MeshRenderer;
 private var nextTextureChange = 0.0;
 var changeRate = 0.5;
    
 function Update(){    
 if (Time.Time > nextTextureChange)
 {
 renderer.enabled = !renderer.enabled;
 nextTextureChange = Time.Time + changeRate;
 }
 }
 
Comment
Add comment · Show 8 · 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 S1LB0T · Jun 11, 2013 at 07:09 PM 0
Share

Thanks for the info and the code!! I'll have to plug it in and see what I can make happen with it :)

avatar image S1LB0T · Jun 11, 2013 at 08:48 PM 0
Share

now the console is telling me that this line: "nextTextureChange = Time.Time + changeRate;" "NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)"

And yes, the script is attached to the lightglow object. There is also something about the code that makes the perfomance level of my game drop drastically upon entering play mode. This did not happen with my original script, and if it is because of syntax errors (like you mentioned before) I have no idea how to tell, because I know nothing about C#.

Here's a little more in-depth info about what I'm trying/wanting to do: Currently, one in-game day lasts 15 $$anonymous$$utes (900 seconds). This is using a "GameTime" C# script (that I did not write). There are set varibales for sunrise(220-225 sec) and sunset(670-675 sec). I know you said time.time cant be "reset" but there is something in the GameTime script that resets the "time of day" counter back to 0 after 900 seconds (or however long the daycycle is set to), but I've had no luck trying to find it within the GameTime script itself. I'm trying to enable the light glow right before sunset and then disable it right after sunrise, then I want this process to repeat.

avatar image AmoralAckbar · Jun 11, 2013 at 08:56 PM 0
Share

Did you copy the code exactly? I have a feeling if you did it is not going to work. That particular error might be due to using .Time ins$$anonymous$$d of .time (I mistyped it with a capital). Take a look at the unity documentation for Time.time, it actually has a code block that does a time based projectile that looks just like $$anonymous$$e.

avatar image S1LB0T · Jun 11, 2013 at 09:17 PM 0
Share

No I didn't copy it exactly, I'll have to look around some more. In any case, thanks.

avatar image AmoralAckbar · Jun 11, 2013 at 09:17 PM 0
Share

Okay at this point, my suggestion will be to get a better understanding of how that GameTime script is working and then take advantage of it. $$anonymous$$ost likely, the internal clock for that script is what you want to be accessing from your LightGlow object.

However, the code that you can find here http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html is doing a time based projectile creation. Utilizing the same method would let you tweak it to do what you want (the code I supplied is doing this).

Show more comments

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

15 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

Related Questions

Initiating several events on collision 1 Answer

How do I destroy a game object when it is hit by a weapon? 3 Answers

Custom Inspector Invocation behaviour? 1 Answer

timed bomb, explosion detection 2 Answers

button reapats whats in its code 2 Answers


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