• 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
1
Question by Showken · Oct 14, 2010 at 02:31 AM · as-operator

how to use "as"

I read some tutorial.but didn't understand its usage.

GUITween temp = null;
temp = GameObject.Instantiate(guiTween) as GUITween;

what the different between with "as" and without "as",thank you!

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

Answer by Jessy · Oct 14, 2010 at 02:51 AM

First, you don't need to use "GameObject." Instantiate by itself works fine, as long as your class derives from Object, which is very likely the case.

To actually answer your question, the reason you need as is that Instantiate returns an Object, not a GUITween. So you need to tell Unity that you want that object to have all the power of a GUITween, not just that of an Object. You also want to be able to store the returned object as a GUITween. as does all of this.

Here is some more information about as.

Comment
Add comment · 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
-1

Answer by Alexey Elyakov · Oct 14, 2010 at 04:34 AM

For illustrate.

Try wrote "#pragma strict" on top of code. You will get error, if run code without "as". Because with "#pragma strict" it not supports dynamic cast between classes.

Comment
Add comment · Show 5 · 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 · Oct 14, 2010 at 04:41 AM 0
Share

You're talking about Unityscript, but the code here is C#.

avatar image Alexey Elyakov · Oct 14, 2010 at 05:24 AM 0
Share

Really? Hmm, well then sorry.

avatar image Alexey Elyakov · Oct 14, 2010 at 05:50 AM 0
Share

Why code not be Unityscript?

avatar image Eric5h5 · Oct 14, 2010 at 05:56 AM 0
Share

"GUITween temp = null;" is not Unityscript.

avatar image Alexey Elyakov · Oct 14, 2010 at 06:01 AM 0
Share

Thanks. I'll know.

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

No one has followed this question yet.

Related Questions

grab weapon script problem 1 Answer

transform array gives error. why? 1 Answer

why the "GetComponent()" add "as" sometimes? 1 Answer

TextMesh and pragma strict together, issue 1 Answer

Integer value casting to KeyCode enum in JS? 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