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 xwolfhunter · Jul 20, 2012 at 12:57 AM · variabletime

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!

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

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.

Comment
Add comment · Show 6 · 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 xwolfhunter · Jul 21, 2012 at 12:22 AM 0
Share

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?

avatar image Seth-Bergman · Jul 21, 2012 at 12:26 AM 0
Share

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

avatar image Eric5h5 · Jul 21, 2012 at 12:26 AM 0
Share

$$anonymous$$y answer above is declaring a variable, "`timeSinceLastShot = Time.time`" is initializing it to the given value.

avatar image xwolfhunter · Jul 21, 2012 at 12:27 AM 0
Share

Wait, would it be something like this:

var timeSinceLastShot : Time;

function Awake(){ timeSinceLastShot = Time.time; }

avatar image Seth-Bergman · Jul 21, 2012 at 12:30 AM 0
Share

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)

Show more comments
avatar image
0

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)

Comment
Add comment · 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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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