• 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
Question by lomso · Jan 16, 2012 at 07:45 PM · beginnerpragmastrict

pragma strict

I'm trying to understand pragma strict but I'm a bit confused. I thought it will report compile errors when I don't define the type and disable dynamic typing. But why do I not get any errors in this code...

 #pragma strict
 var lol;
 function Awake()
 {
     lol = 45+5;
     lol = "wtf";
 }
Comment
Demigiant
Meater6
cregox
numberkruncher
wip
Abra Kadabra

People who like this

6 Show 0
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

3 Replies

  • Sort: 
avatar image

Answer by Eric5h5 · Jan 17, 2012 at 12:24 AM

With #pragma strict, you will get errors in this code:

 function Blah (varA, varB) {

You will also get errors in this code:

 GetComponent("SomeScript").SomeFunction();

However, it's true that the code you posted will compile and run (slowly). Personally I'd consider that a bug, and that #pragma strict shouldn't allow it to compile.

Comment
Demigiant
Meater6
cregox
numberkruncher
Abra Kadabra

People who like this

5 Show 0 · 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

Answer by dannyskim · Jan 16, 2012 at 09:07 PM

Unity dynamic typing still can pick up on basic variables such as integers and strings. Pragma strict really comes in handy when you're casting anything that is in direct correlation with the Unity framework, such as a GameObject versus a Transform.

From the docs:

Use #pragma strict

Now the problem is of course, that you don't usually notice when you are using dynamic typing. #pragma strict to the rescue! Simply add #pragma strict at the top of a script and Unity will disable dynamic typing in that script, forcing you to use static typing. Wherever a type is not known, Unity will report compile errors. So in this case, foo will produce an error when compiling:

Now in regards to you first using it as an integer and then using it as a string, I'm going to assume that Unity's implementation of javascript is dynamically changing the type on the fly as well. Although Unity's js is extremely fast because of the way they designed it and is probably easier to use than c#, I still prefer c# because of syntax and much stricter casting parameters as to prevent any possible mishaps like this in your code.

So, like I said, I can only assume that it's recasting the variable on the fly for you as to not error out.

Comment
GameGuy
Meater6
Crispinator

People who like this

3 Show 1 · 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 lomso · Jan 16, 2012 at 11:57 PM 0
Share

i see. thanks :)

avatar image

Answer by Ingy · Feb 14, 2016 at 02:04 AM

Use #pragma script at the top of your scripts when Javascripting

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

ios cant access variables in array 1 Answer

#pragma strict problem 1 Answer

Can I make a list of hashtables or classes with pragma strict 2 Answers

Can someone list every possible basic variable? 3 Answers

How to change a texture of a gameobject by collider trigger ? 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