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 moonLite · Nov 04, 2012 at 07:04 PM · javascriptvariabletype

What is typed variable?

Hi Guys,

1) What is typed variable?
2) And Examples of it?
3) which part of my code can be replaced with typed variables?
4) and how does it speed up my game scene process?
5) please do let me know if there's anything else I need to know in order to understand typed variables better, thanks!

I got this question from my previous question, one of person suggested to me to use typed variables, but I don't know what is it.
http://answers.unity3d.com/questions/282486/what-does-this-for-loop-mean.html

Below is coding( in javascript / unityscript) :

         private var ttag = "enemyAim";
         private var target : Transform;
 
         var closestEnemy : Transform = null;
         var dist : float;
 
         function getClosestEnemy () : Transform
         { 
          var taggedEnemys = GameObject.FindGameObjectsWithTag(ttag);
          var closestDistSqr = Mathf.Infinity;
          var closestEnemy : Transform = null;
          print("get closestEnemy");
 
          for (taggedEnemy in taggedEnemys) // this part (1)
          {
           var objectPos = taggedEnemy.transform.position;
      dist = (objectPos - transform.position).sqrMagnitude;
 
  if (dist < 3.0)
     {
  if (dist < closestDistSqr)
    {
     closestDistSqr = dist;
     closestEnemy = taggedEnemy.transform;
    }
      }
           }
          target = closestEnemy;
         }

Thanks in advance!

Comment
Add comment · Show 2
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 Montraydavis · Nov 04, 2012 at 07:20 PM 0
Share

Uhm . . . All variables have types . . . Are you asking about a specific type, or . . .?

avatar image Montraydavis · Nov 04, 2012 at 07:35 PM 0
Share

$$anonymous$$aybe take a look at this for more information: http://docs.unity3d.com/Documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html

2 Replies

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

Answer by Eric5h5 · Nov 04, 2012 at 11:43 PM

All variables are automatically typed for you, as long as you supply a value. You can optionally add types to your code where it doesn't have them. For example:

 private var ttag = "enemyAim";

can be written as

 private var ttag : String = "enemyAim";

However this has no actual performance benefit, since both lines of code compile to the exact same thing. The compiler sees that you are assigning a string to the ttag variable, therefore it is typed as String. Writing out the type may help you understand the code better in some cases, but it's optional. The only thing you should avoid doing is neglecting to supply both a type and a value, such as this:

 var foo; // bad!

This can still work in some cases (desktop platforms where you're not using #pragma strict), however it will be quite slow since the type has to be figured out at runtime. As long as you supply a type or a value (or both), then you're fine.

(The person you're referring to in the comments to your other question is mistaken, by the way. All of your variables are typed.)

Comment
Add comment · 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 moonLite · Nov 06, 2012 at 06:59 AM 0
Share

@Eric5h5 , thanks Eric! I was wondering what did I didn't declared as type! heheh thanks again!

your explanation really helped me in understand e var type better and what not to do!

For others, for question 1 & 2, please see answer above too!

avatar image
1

Answer by Demigiant · Nov 04, 2012 at 09:58 PM

1) A typed variable is a variable that defines its type (Transform, Vector3, etc) explicitly. It is defined by adding ":VariableType" after the name of the variable.

2) For example, this is a variable:

 var myPosition;

While this is a typed variable:

 var myPosition:Vector3;

3) Everything that says "var" con be assigned a type, as shown above

4) If the engine knows the type of a variable, it won't need to lose time to "guess" it, which leads to a faster performance. Also, you'll notice that more errors are reported to you while you code instead than while you run the game, which is very good.

Comment
Add comment · 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 moonLite · Nov 06, 2012 at 06:58 AM 0
Share

@$$anonymous$$e , thanks! it really helped me in understand my question's answers very clearly! I wish I could mark 2 answers but I cant. So I thumb this one up.

For others, for question 3 & 4, please see below answer.

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

13 People are following this question.

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

Related Questions

How could you access a script of varying name? 5 Answers

Variable Type for an Instance of any Script 2 Answers

Lower player's health from separate script. 2 Answers

Variable Not Changing. 0 Answers

InputField in a different scene to Text 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