• 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 deckbottom · Apr 09, 2012 at 12:16 AM · getcomponentglobal#pragma strict

#pragma strict and GetComponent()

I am learning unity using tutorials. I learned the #pragma strict when using GetComponent generates this error: Assets/scriptObjA.js(12,69): BCE0019: 'foundVar' is not a member of 'UnityEngine.Component'.

MonoDevelop-unity adds the #pragma strict to my java script as default. is not used in the tutorials, is it a good practice to use it?

When I remove #pragma strict from the script, the code works correctly. Should I be concerned? Is there a better technique to create/access global variable/functions?

I have parsed the code down from the tutorials down to this example: The error is generated accessing a variable found in a script that is a component of a prefabsceneManager from an object called prefabObjA. Here are the two scripts:


scriptSceneManager.js


 var foundVar : float = 20;
 
 function Start () {
 }
 
 function Update () {
 }
 
 function FoundMe () {
 }



scriptObjA.js


 var varK         : int = 3;
 var lookFor     : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     print( lookFor.transform.GetComponent("scriptSceneManager").name );
     print( lookFor.transform.GetComponent("scriptSceneManager").foundVar );
 
 }



I am assigning the variable lookFor in the interface with the prefabSceneManager that has scriptSceneManager.js as a component. Thanks in advance for your help/advice.

Comment
Add comment · Show 1
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 aldonaletto · Apr 09, 2012 at 01:30 AM 0
Share

You should use the typed version of GetComponent to avoid these errors:

   print( lookFor.transform.GetComponent(scriptScene$$anonymous$$anager).name );
   print( lookFor.transform.GetComponent(scriptScene$$anonymous$$anager).foundVar );

The string version of GetComponent can find the specified type and its members at runtime, but is much slower - as @Eric5h5 said. It's better to use it only when the target script isn't available at compile time (when it's written in a different language, for instance).

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Apr 09, 2012 at 12:52 AM

Yes, you should use #pragma strict. It's required when publishing to non-desktop platforms (iOS etc.) anyway, so if you intend to do that at some point, it's best to be used to it. As for the actual problem, never use strings with GetComponent. It's slower and makes GetComponent return Component instead of the actual type.

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 Kleptomaniac · Apr 09, 2012 at 01:27 AM 0
Share

Adding on from what Eric said, just so you know, #pragma strict disables dynamic typing and will throw compiler errors if it finds any. In case you didn't know, dynamic typing is a feature of the compiler which allows it to cast the most appropriate type based on how you use a variable. This is an unnecessary waste of memory allocation, however. You should in all cases use static typing. As this states, however, sometimes you can accidentally do it. That's where #pragma strict comes in. So, yes, it is best to get into the practice of using #pragma strict. Even if it's kind of irritating cleaning up errors which could be avoided, your game will most definitely come up better in the long run. :)

avatar image deckbottom · Apr 09, 2012 at 02:02 AM 0
Share

Thanks! I had just tried that. Working great. I cannot believe the time spent to get to the #pragma strict solution, I wish I had posted earlier.

avatar image Kleptomaniac · Apr 09, 2012 at 02:31 AM 0
Share

Please don't post comments as answers. :)

avatar image Eric5h5 · Apr 09, 2012 at 02:42 AM 0
Share

@$$anonymous$$leptomaniac: That's incorrect. Dynamic typing is not a compiler feature, it's a run-time feature, which occurs when a variable is given no type. The appropriate type is figured out on the fly, which is about 20X slower than static typing, but I don't know that there's any memory penalty. Example:

 var foo;

Type inference is a compiler feature, in which a variable is statically typed based on the value supplied. This is identical to manual static typing, and has no penalty of any kind (though I suppose it takes fractionally longer to compile). Example:

 var foo = Vector3.zero;

It's perfectly acceptable to do that, as long as it's clear from the value what the type is (such as the case above), and it doesn't conflict with #pragma strict at all.

avatar image Kleptomaniac · Apr 09, 2012 at 02:51 AM 0
Share

O$$anonymous$$ thanks for that clear up Eric. I tend to give a lot of erroneous information. :D

However, I think that it's probably best at least for consistency and just in case there are some problems with #pragma strict, that people should use static typing wherever possible. Better safe than sorry?

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Trouble accessing scripts (Boo) 1 Answer

Variable from public class to script 1 Answer

Passing a Script Name to a Function 2 Answers

GetComponent Picking up multiple scripts from differrent objects? 1 Answer

Accessing a list on other script 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