- Home /
How to properly declare this variable
How do I properly declare this variable?
var timeSinceLastShot = Time.time;
It works just fine, but always gives me this error:
ArgumentException: get_time 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.
SentryShootUp..ctor () (at Assets/Bullets/Sentry Bullets/Sentry Autofire Scripts/SentryShootUp.js:6)
(Sorry about the long lines)
I know that the commonly used variable declaration
var nameHere = gameObject;
works just fine, but has some issues with the system, so should be written as such:
var nameHere : GameObject;
I feel as though it's a similar situation with the whole Time.time
thing, but I don't know how to write it properly. I have tried this:
var timeSinceLastShot = Time;
timeSinceLastShot = Time.time;
but that didn't work. I also read the error, which told me to declare it in the Start or Awake function, but that's quite obviously kinda silly, since it won't be accessible in the Update function if I do that (Which is the only place it's actually used in the script).
So, that's about it. I think I've given all the information I can. Thanks for any answers!
Answer by Eric5h5 · Jul 20, 2012 at 01:09 AM
var timeSinceLastShot : float;
Only declare variables outside functions rather than trying to run code; Time.time will vary during runtime so it's not something that can be declared in the inspector. There's nothing silly about initializing the variable during Start or Awake, and that is in fact what you should do. Note that declaring a variable and initializing it are two different things.
Hmm . . . I'm kinda new to this, so I don't know how to separate declaring and initializing variables, nor how to go about doing it in a script. How would I go about this?
you can use the Start function:
var timeSinceLastShot : float;
function Start(){ timeSinceLastShot = Time.time; }
if you're trying to update it every frame though, ins$$anonymous$$d of start use the Update function
$$anonymous$$y answer above is declaring a variable, "`timeSinceLastShot = Time.time`" is initializing it to the given value.
Wait, would it be something like this:
var timeSinceLastShot : Time;
function Awake(){ timeSinceLastShot = Time.time; }
Time.time is represented as a float, that is a floating point (with a decimal) as opposed to an integer
that's why we create a float to store its value
(as in my answer above)
Answer by Seth-Bergman · Jul 21, 2012 at 12:28 AM
you can use the Start function:
var timeSinceLastShot : float;
function Start(){ //this is used for initialization, as it is only called
timeSinceLastShot = Time.time; //once, at the beginning
}
if you're trying to update it every frame though, instead of start use the Update function
function Update(){ //this updates every frame. If you want to keep track of
timeSinceLastShot = Time.time; //Time.time dynamically, use this
}
EDIT:
to clarify though, if the var "timeSinceLastShot" is meant to track the time since the last event (such as a player gunshot), you would need to store it at the moment of the shot, as a separate float, to compare to the current time:
var timeSinceLastShot : float;
var mostRecentShotTime : float;
function Update(){
timeSinceLastShot = Time.time - mostRecentShotTime;
if(Input.GetButtonDown("Fire1")
{
mostRecentShotTime = Time.time;
Shoot();
}
}
function Shoot(){
///gunshot code here }
hope this helps (edit:sorry, got distracted, fixed)
Your answer
Follow this Question
Related Questions
Delaying a dynamic Variable(transform.position for Ex.) 2 Answers
Played time? 2 Answers
Creating a variable jump using deltaTime in C#. 0 Answers
timed value increase? 1 Answer
Time variables not interacting 1 Answer