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 ksi · May 13, 2012 at 04:17 PM · programming-basics

us of the "as" keyword (Instantiate, GetComponent)

Following tutorials I notice the use of the "as" keyword. I didn't find it in the programing reference. Its usage seems rather self explanatory but would like to get more information for my own understanding; I don't like to write blindly without understanding nor being unable to find the reference

 var player1 : GameObject = Instantiate(playerDeathObj, dpos, playerDeathObj.transform.rotation) as GameObject ;

The other exemple is more important as it seems that the "AS" Solved the issue I was encountering :

 var script1 : shipController = player1.transform.gameObject.GetComponent("shipController") as shipController ;


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
3

Answer by Drakestar · May 13, 2012 at 04:26 PM

Generally, the 'as' keyword performs a type conversion (a "cast") that evaluates to null if the conversion fails (as opposed to throwing an exception). That way, you can do a "if (x != null) check in the next line and react depending on whether the cast succeeded or not.

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 Drakestar · May 13, 2012 at 04:31 PM 0
Share

BTW, that answer is C# specific. In Javascript, the as might not actually be necessary and only be there for good form. I don't use Javascript regularly.

avatar image
0

Answer by Eric5h5 · May 13, 2012 at 04:31 PM

The "as" keyword is used to cast the result to a specific type. However, in neither of those cases is it actually needed. Instantiate (in Unityscript) already returns the type of the prefab anyway, so it's completely unnecessary in the first example. Just remove it, since it's not doing anything.

 var player1 : GameObject = Instantiate(playerDeathObj, dpos, playerDeathObj.transform.rotation);

In the second example, GetComponent should not use strings (in almost all cases). With strings, GetComponent returns Object, so it needs to be cast, but without strings, GetComponent returns the type of the component. So it should read

 var script1 : shipController = player1.transform.gameObject.GetComponent(shipController);

(Although "player1.transform.gameObject" is superfluous. Just do "player1.GetComponent(shipController)".)

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 Drakestar · May 13, 2012 at 04:35 PM 0
Share

In the first example, the 'as' might help with UnityScript's static typing. Not necessary, but beneficial for runtime performance. This is just a guess without knowing any of the implementation details of the UnityScript compiler.

avatar image Eric5h5 · May 13, 2012 at 04:44 PM 0
Share

@Drakestar: no, it won't help at all. As I mentioned, Instantiate in Unityscript already returns the type of the prefab being instantiated anyway. Using "as" in this case won't hurt, but it won't help either, so you might as well not bother with it; it's completely redundant.

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

6 People are following this question.

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

Related Questions

How To Design Scripts/Systems 1 Answer

Object keeps slowing down for no apparent reason 1 Answer

open a door from an animation 1 Answer

"tex.SetActive(false);" Why is this wrong? 1 Answer

New to unity and any coding for that matter, need advice!!! 3 Answers


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