Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DragonautX · Jun 17, 2016 at 08:41 PM · programmingsyntax

What is "(GameObject)" in "(GameObject)Instantiate(...)" for?

I came across this line of code while doing Unity's Multiplayer Networking tutorial:

var enemy = (GameObject)Instantiate(enemyPrefab, spawnPosition, spawnRotation);
What is the part "(GameObject)" called? Is there another way to write this same line of code?

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

1 Reply

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

Answer by phxvyper · Jun 17, 2016 at 09:02 PM

Whenever you have a type name in parentheses like that, before a value, its called an Explicit Cast.

Casting an object is when you "convert it" - for lack of a better term - to a different type that it can be converted to.

You can cast ints to floats, you can cast all types to object, you can cas List to IList, etc:

 int someInt = 10;
 SomeClass someObject = new SomeClass("this is a parameter");
 List<string> someListOfStrings = new List<string>();
 
 float intAsFloat = (float)someInt;
 object someClassAsObject = (object)someObject;
 IList stringListAsIList = (IList)someListOfStrings;
 
 intAsFloat == 10f : true
 someClassAsObject == someObject : true
 stringListAsIList == someListOfStrings : true

Comment
Add comment · Show 10 · 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 DragonautX · Jun 17, 2016 at 10:32 PM 0
Share

Okay. As long as it can be converted to that type, I can use that type. Is an Explicit Cast the same as something like "Instantiate(...) as GameObject"?

avatar image jgodfrey DragonautX · Jun 17, 2016 at 10:55 PM 2
Share

If the cast succeeds, an explicit cast and a "as" cast results are the same. However, they differ if the cast fails. In that case, the explicit cast will throw an exception while the "as" cast will simply return null. There are some other nuances, but that's the important difference.

avatar image SarperS DragonautX · Jun 17, 2016 at 11:31 PM 1
Share

Use the "as" keyword when you want to implement further behaviour if the casting fails. Explicit casting will throw an InvalidCastException if the casting fails. But with the "as" operator you have the option to do below;

 var enemy = Instantiate(enemyPrefab, Vector3.zero, Quaternion.identity) as GameObject;
 if(enemy == null) {
 // Do something else or display a verbose debug log stating what's gone wrong
 return;
 }

Also note that you can't cast value types with the "as" keyword because they can't have the value "null".

avatar image DragonautX SarperS · Jun 17, 2016 at 11:44 PM 1
Share

Okay thanks @jgodfrey and @Sarper Soher. I'll use an Explicit Cast for error checking and the "as" keyword if I know I want to do error handling.

Show more comments
avatar image Bunny83 · Jun 18, 2016 at 12:28 AM 0
Share

@phxvyper: All your casting examples are "bad" examples for an explicit cast since they are all upcasts which are always implicit. It's not wrong to use an explicit cast here just unnecessary.

A downcast (like the one in the question) always requires an explicit cast in C#. Every class can be treated implicitly as a less defined type (base type). However a base class reference can't be treated as a more defined type without a cast. It always the same logic. Every "Dog" is an "Animal" (since Dog is derived from Animal), but not every Animal is a Dog. If you have a reference to an Animal which actually is a Dog, you need to downcast the reference explicitly to Dog in order to access Dog specific members.

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

51 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 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 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 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

Multiple Cars not working 1 Answer

Are there any draw backs to using this kind of Switch statement? 2 Answers

error ... not a UnityEngine.Component 1 Answer

Help with syntax error 1 Answer

warning CS0414: The private field `ChangeToSecondForm.playerToBeChangedFrom' AND `ChangeToSecondForm.playerChangingTo' is assigned but its value is never used 2 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