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
3
Question by Goldragon979 · Jun 22, 2010 at 11:37 PM · variablescriptingbasicsclassname

why do I need to type variables classes?

Unity seems to work, wether I type float or boolean (for example) after class name or not.

Could you explain me why I need to type it?

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

Answer by dhendrix · Jun 22, 2010 at 11:43 PM

When using Javascript in Unity, you don't have to explicitly declare types. Such as:

var myInt = 5;

It's smart enough to know that you are declaring an int in this case. There are times when it can't figure out at compile time, so instead it has to figure out and typecast your variables at runtime, which can be costly in performance.

If you put "#pragma strict" at the top of your scripts, an error will be thrown if the type cannot be determined at compile time. Use this if you are worried about that little bit of extra performance.

Comment
Add comment · Show 2 · 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 Mike 3 · Jun 22, 2010 at 11:45 PM 0
Share
  • for using the same variable name as me (before i even hit post! :P) and adding the pragma strict comment

avatar image Pirate___man · Jun 22, 2010 at 11:47 PM 0
Share

I would say it is just good habit! :)

avatar image
1

Answer by Mike 3 · Jun 22, 2010 at 11:44 PM

Assuming you mean variable names:

Javascript's compiler has the ability to (most of the time) implicitly assign the right type to the variable

In those cases, you don't need to specify it

On the other hand, sometimes it can't, and it's forced to try guess the type at runtime

This can lead to some spectacular runtime exceptions, and even when it works, it's slower than typing it yourself.

The best practice is to type anything that isn't explicitly defined by the function or variable you're assigning to it

e.g.

var myInt = 0; //sets myInt to be an int

var myInt = FunctionThatReturnsAnIntExplicitly(); //sets myInt to be an int

generally you should never create objects like this:

var myInt;

It's asking for trouble

Comment
Add comment · Show 3 · 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 Eric5h5 · Jun 22, 2010 at 11:57 PM 0
Share

Type inference is not slower than explicit typing. No matter whether you specify the type or the compiler infers it, it results in the exact same thing and generates the exact same code. You may be confusing type inference with dynamic typing, but they are two very different concepts.

avatar image Mike 3 · Jun 23, 2010 at 12:01 AM 0
Share

Not confusing it at all. I specifically said it happened when it couldn't infer the type

avatar image Eric5h5 · Jun 23, 2010 at 12:56 AM 0
Share

Oh, I see what "it" is referring to now. It seemed like you were referring to the implicit typing.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Referencing variables / functions on another script 1 Answer

Creating & accessing a function from a diffrent "OnTriggerEnter" Function 1 Answer

Find Object with variable, not a name. 1 Answer

C# Accessing Variables from Class Variable 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