Problem with

I have this code

var timer : float = 1 * Time.deltaTime;



function Update ()

{

	if(timer >= 60)

	{

		timer -=60;

	}

	

	if((timer >= 1.805)  (timer <= 30))

	{

	light.intensity = 0;

	}

	else

	{

	light.intensity = 1;

	}

}

But I get these errors

ArgumentException: get_deltaTime can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
LightOnOff…ctor () (at Assets/Script/LightOnOff.js:1)

UnityEngine.Time:get_deltaTime()
LightOnOff:.ctor() (at Assets\Script\LightOnOff.js:1)

You can’t call Unity functions from default Initializers (default Initializer being the assigning a class variable when declaring it).

Assign timer in an Awake function. This is because Unity uses Constructors for serializing objects, and can be called at any time, from different threads.

In this particular issue a loading thread tried to access Unity functions improperly, because it was creating the objects.

So what shall i instead

Assign the value to timer in a function called Awake()

i havn’t use Awake() before

var timer : float;
function Awake ()
{
timer = Time.deltaTime;
}

Call it like other functions. It gets called once when the object is created:

Hmmm… Lets look at what the code is supposed to do.

It is a basic “blink” timer… from 1.805 seconds to 30 seconds (out of 60 seconds) a light is supposed to be off…

I dont know why the odd timing. So lets do it in a very simple manner to help you get a grasp on it.

function Start(){
	while(true){
		light.intensity = ((Time.time / 30)  1) == 1 ? 0 : 1;
		yield;
	}
}

//time == 00 seconds / 30 = 0.0  1 = 0
//time == 10 seconds / 30 = .333  1 = 0
//time == 20 seconds / 30 = .666  1 = 0
//time == 30 seconds / 30 = 1.0  1 = 1
//time == 40 seconds / 30 = 1.333  1 = 1
//time == 50 seconds / 30 = 1.666  1 = 1
//time == 60 seconds / 30 = 2.0  1 = 0
//time == 70 seconds / 30 = 2.333  1 = 0
//time == 80 seconds / 30 = 2.666  1 = 0
//time == 90 seconds / 30 = 3.0  1 = 1

Basically we are working with a relational time from zero to 30 seconds. When it is beyond that point, then our light turns on. We want it to cycle, so we divide it by 30 seconds and then binary AND that number to 1. So if the result of this is one… then our light is on, if it is not… it is off.

Binary functions can be incredibly useful. In this case they excel.

Time.deltaTime multiplies a number by the amount of time a frame lasts, so the only place to use it is in Update.

A simple option would be to change your first line to:

var timer : float = 1.0;

Then add the following line to the beginning of Update:

timer += Time.deltaTime;

Okay thanks for the helps :wink: No errors anymore :wink:

But now I would like to check what the timer is all the time :wink:

so i have the code

var timer : float;



function awake ()

{

timer = Time.deltaTime;

}



function Update ()

{

	if(timer >= 60)

	{

		timer -=60;

	}

	

	if((timer >= 1.805)  (timer <= 30))

	{

	light.intensity = 0;

	}

	else

	{

	light.intensity = 1;

	}



Debug.Log(timer);

}

But when i Debug.Log the timer, then it just comes out with " 1 ", why?

Awake is only called once so you’re never updating the value of timer. Moonjump suggested the correct solution.

The function Awake is only called once, so timer only gets set when the object is created, and then never gets updated ever again. Put timer = Time.deltaTime in your update function.

P.S. Your name is obviously ironic.

JustinLloyd Thanks for help;)
Yeah my Name is true, when we talk about my skill with playing games, but make the games, it is idd ironic :wink:

The finaly code

var timer : float = 1;





function Update ()

{

timer += Time.deltaTime;

	if(timer >= 60)

	{

		timer -=60;

	}

	

	if((timer >= 1.805)  (timer <= 30))

	{

	light.intensity = 0;

	}

	else

	{

	light.intensity = 1;

	}



Debug.Log(timer);

}