- Home /
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.
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;
}
}
Thanks for the info and the code!! I'll have to plug it in and see what I can make happen with it :)
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.
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.
No I didn't copy it exactly, I'll have to look around some more. In any case, thanks.
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).
Your answer
Follow this Question
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